Change Status Bar Color
For non-UINavigationBar status bars
Section titled “For non-UINavigationBar status bars”- In info.plist set
View controller-based status bar appearancetoYES - In view controllers not contained by
UINavigationControllerimplement this method.
In Objective-C:
- (UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent;}In Swift:
override func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.LightContent}For UINavigationBar status bars
Section titled “For UINavigationBar status bars”Subclass UINavigationController and then override these methods:
In Objective-C:
- (UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent;}In Swift:
override func preferredStatusBarStyle() -> UIStatusBarStyle { return .lightContent}Alternatively, you can set barStyle on the UINavigationBar instance:
Objective C:
// e.g. in your view controller's viewDidLoad method:self.navigationController.navigationBar.barStyle = UIBarStyleBlack; // this will give you a white status barSwift
// e.g. in your view controller's viewDidLoad method:navigationController?.navigationBar.barStyle = .black // this will give you a white status barUIBarStyle options are default, black, blackOpaque, blackTranslucent. The latter 3 should all give you a status bar with white text, just the last two specify the opacity of the bar.
Note: you can still change the appearance of your navigation bar as you like.
If you cannot change ViewController’s code
Section titled “If you cannot change ViewController’s code”If you are using library that contains (for example) AwesomeViewController with a wrong status bar color you can try this:
let awesomeViewController = AwesomeViewController() awesomeViewController.navigationBar.barStyle = .blackTranslucent // or other styleFor ViewController containment
Section titled “For ViewController containment”If you are using UIViewControllerContainment there are a few other methods that are worth looking at.
When you want a child viewController to control the presentation of the status bar (i.e. if the child is positioned at the top of the screen
in Swift
class RootViewController: UIViewController {
private let messageBarViewController = MessageBarViewController()
override func childViewControllerForStatusBarStyle() -> UIViewController? { return messageBarViewController }
override func viewDidLoad() { super.viewDidLoad()
//add child vc code here...
setNeedsStatusBarAppearanceUpdate() }}
class MessageBarViewController: UIViewController {
override func preferredStatusBarStyle() -> UIStatusBarStyle { return .Default }}Changing the status bar style for the entire application
Section titled “Changing the status bar style for the entire application”
SWIFT:
Section titled “SWIFT:”Step 1:
Section titled “Step 1:”In your Info.plist add the following attribute:
View controller-based status bar appearanceand set its value to
NOas described in the image below:
Step 2:
Section titled “Step 2:”In your AppDelegate.swift file, in didFinishLaunchingWithOptions method, add this code:
UIApplication.shared.statusBarStyle = .lightContentor
UIApplication.shared.statusBarStyle = .defaultThe **.lightContent** option will set the colour of the **statusBar** to white, for the entire app.
The **.default** option will set the colour of the **statusBar** to
the original black colour, for the entire app.
OBJECTIVE-C:
Section titled “OBJECTIVE-C:”
Follow the first step from the SWIFT Section.
Then add this code to the AppDelegate.m file:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];or
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
