0
votes

I don't know how can I set the background color of UIStatusBar as black in iOS7 while background color of navigation bar is not black.and as in iOS7 navigationbar is tranparent so status bar get backgroundcolor from background color of UINavigationBar.and text over statusbar should be of white color.I have used UIStatusBarStyleBlackOpaque but it is also deprecated in iOS7.I know I can change the textcolor of statusbar as white by setting StatusBarStyle as UIStatusBarStyleLightContent.

And for setting the background color of UIStatusBar as black i have used below code:-

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        UIView *addStatusBar = [[UIView alloc] init];
        addStatusBar.frame = CGRectMake(0, 0, 320, 20);
        addStatusBar.backgroundColor = [UIColor blackColor];
        [self.window.rootViewController.view addSubview:addStatusBar];
    }

By above code i have manually added uiview in place of status bar of needed color. The problem is that by this uistatusbar is gone to back of the black view i.e. addStatusBar and thus text over status bar is hidden.

How to solve it?

Please Help !! Thanks in Advance !!

3

3 Answers

3
votes

I Just added this line to make navigationbar non-translucent.

[navC.navigationBar setTranslucent:NO];

and use the same above code to add uiview in place of statusbar with needed color.with lightcontentstyle to show the text in white color. It solved my problem.

0
votes

Goto your app info.plist

1) Set View controller-based status bar appearance to NO 2) Set Status bar style to UIStatusBarStyleLightContent

Then Goto your app delegate and paste the following code where you set your Windows's RootViewController.

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 20)];
    view.backgroundColor=[UIColor blackColor];
    [self.window.rootViewController.view addSubview:view];
}

Hope it helps.

0
votes

You can set/change background color for status bar during application launch or during viewDidLoad of your view controller.

I tried with iOS 11 (with Swift 4) here:

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}


// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.cyan
        return true
    }
}


or 
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.cyan
    }

}



Here is result:

enter image description here