In my iPhone application built with Xcode 5 for iOS 7 I set UIViewControllerBasedStatusBarAppearance=YES
in info.plist
, and in my ViewController
I have this code:
-(UIStatusBarStyle) preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
But the status bar is still black against the black background.
I know its possible to change this app-wide by setting UIViewControllerBasedStatusBarAppearance=NO
in info.plist
, but I actually need to alter this on a viewController
by viewController
basis at runtime.
For swift 3, in your UIViewController:
To provide more detail into the accepted answer, put the following line in your app delegate's
didFinishLaunchingWithOptions:
method:Then, in your Info.plist, add
View controller-based status bar appearance
and set it toNO
.I believe that's how it should be done, NOT from the navigation controller, if you want the same status bar color for the entire app. You might have screens that are not necessarily embedded in a
UINavigationController
, or a differentUINavigationController
subclass somewhere else, and other things.EDIT: You can also do it without typing any code: https://stackoverflow.com/a/18732865/855680
I discovered that if your ViewController is inside a navigationController then the navigationController’s
navigationBar.barStyle
determines the statusBarStyle.Setting your navigationBar’s
barStyle
toUIBarStyleBlackTranslucent
will give white status bar text (ie.UIStatusBarStyleLightContent
), andUIBarStyleDefault
will give black status bar text (ie.UIStatusBarStyleDefault
).Note that this applies even if you totally change the navigationBar’s color via its
barTintColor
.I may be coming to this a bit late, but incase anyone else is looking for a working and verified app wide solution.
@mxcl is correct in describing why this is happening. In order to correct it, we simply create an extension (or category in obj-c) that overrides the preferredSatusBarStyle() method of UINavigationController. Here is an example in Swift:
This code simply extracts the first view controller (the root view controller) and unwraps it (in obj-c just check that it is not nil). If the unwrap is successful (not nil) then we grab the rootViewControllers preferredStatusBarStyle. Otherwise we just return the default.
Hope this helps anyone who might need it.
1) One setting for whole project:
If available, remove
UIViewControllerBasedStatusBarAppearance
key-value pair from your info.plist, or setNO
without removing it. If it's not available in your info.plist, do nothing. Default isNO
for this property.Add below code to your AppDelegate.m:
2) Different settings for different View Controllers:
Add
UIViewControllerBasedStatusBarAppearance
key-value pair to your info.plist and set it toYES
.If your View Controller is not embed in to Navigation Controller. Let's say MyViewController. just add code below to your MyViewController.m file. If your View Controller is embed in to Navigation Controller, create a new Cocoa Touch Class and make it subclass of UINavigationController. Let's say MyNC. Select Navigation Controller View on your Storyboard, at right pane; Utilities -> Identity Inspector -> Custom Class -> Class, type "MyNC". After linking Storyboard View with your "MyNC" Cocoa Touch Class, add code below to your MyNC.m:
Swift 4.2