UIStatusBarStyle not working in Swift

I am trying to change the color of the status bar in my Swift application to white, but I hit a brick wall. I have 3 ViewControllers, each of which is embedded in the NavigationController (maybe this is a problem? I already tried to put the code in the NavigationController class.) I tried using both of the following code snippets in the didFinishLaunchingWithOptions file of my AppDelegate.swift, but it didn’t work.

application.statusBarStyle = .LightContent 

and

 UIApplication.sharedApplication().statusBarStyle = .LightContent 

All Docs have to say about this is that UIBarButtonStyle is Int and gave me this enum fragment that didn't help me at all with the implication.

 enum UIStatusBarStyle : Int { case Default case LightContent case BlackOpaque } 

What am I missing?

+74
ios swift xcode6 uistatusbar appdelegate
Jun 16
source share
11 answers

You have two options.

If you want to continue manually adjusting the status bar style, continue to do what you are doing, but you need to add the following key to your info.plist file with a value of NO .

View controller-based status bar status

Or, if you want to continue to use the appearance of the controller-based status bar, instead of setting the status of the BarStyle application, override the preferredStatusBarStyle property in each view controller for which you want to specify the status bar style.

Swift 3

 override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } 

Swift 2

 override func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.LightContent } 
+138
Jun 16 '14 at 1:26
source share

Swift 3.0

in AppDelegate.swift didFinishLaunchingWithOptions

 UIApplication.shared.statusBarStyle = .lightContent 

Info.plist

 View controller-based status bar appearance -> NO 

Swift 2.2

in AppDelegate.swift didFinishLaunchingWithOptions

 UIApplication.sharedApplication().statusBarStyle = .LightContent 

Info.plist

 View controller-based status bar appearance -> NO 
+135
Feb 01 '16 at 13:28
source share

You must install:

navigationController.navigationBar.barStyle = .black

and the text will appear in white

+23
Sep 25 '15 at 9:44
source share

For iOS9.x and Xcode7, just put this inside AppDelegate.swift :

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { UINavigationBar.appearance().barStyle = .Black } 

This will automatically turn your status bar style into. .Lightcontent for all view controllers inside the UINavigationController.

(Also, remove the View controller-based status bar appearance from Info.plist to suppress warnings that you probably see too!)

+18
Sep 28 '15 at 3:47
source share

In Swift 3, the status bar style has changed to a computed property in the UIViewController, which you can override as follows:

 override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent //or default } 
+15
Sep 17 '16 at 15:44
source share

In iOS 9, the following (setStatusBarStyle) is deprecated and you will get a warning if you go this way.

 UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true) 

If you want all status bars to be changed in one shot, try adding the following to your Info.plist. It will also make your launch screen status bar white. Although the code above will not.

 <key>UIStatusBarStyle</key> <string>UIStatusBarStyleLightContent</string> <key>UIViewControllerBasedStatusBarAppearance</key> <false/> 
+12
Feb 10 '16 at 20:22
source share

for me all of the above dind't work until i add:

 self.navigationController?.navigationBar.barStyle = .black; 

So:

  • Set UIViewControllerBasedStatusBarAppearance to YES in .plist
  • In viewDidLoad call self.setNeedsStatusBarAppearanceUpdate();
  • Override preferredStatusBarStyle
    override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
  • In the overridden method, I also set navigationBar.barStyle so finally
    light :
    override var preferredStatusBarStyle: UIStatusBarStyle { self.navigationController?.navigationBar.barStyle = .black;//or default return .lightContent //or default }
    and for black content use the default

Source here and here .

and if that doesn't work, you can try adding the UINavigationController extension :

 extension UINavigationController { override open var preferredStatusBarStyle: UIStatusBarStyle { if let lastVC = self.viewControllers.last { return lastVC.preferredStatusBarStyle } return .default } } 
+8
Oct 21 '16 at 20:13
source share

Strange using Swift 3.1 and XC8.2.1, but all of the above does not work.

What i did is just

 extension UINavigationController { override open var preferredStatusBarStyle: UIStatusBarStyle { get { return .lightContent } } } 

No plist, no other things. NTN

+3
Mar 16 '17 at 12:22
source share

Do not modify your Info.plist. Add this to your ViewController.swift:

 override func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.LightContent } 
+1
Feb 15 '16 at 6:59
source share

In Swift 3.0, you can override getter in the ViewController for View in the controller-based status bar:

 override var preferredStatusBarStyle: UIStatusBarStyle { get { return .lightContent } } 
+1
Mar 09 '17 at 6:35
source share

Step 1. Add to info.plist View controller-based status bar appearance -> NO

Step 2. Add code to the method where you need to change the color of the status bar:

 UIApplication.shared.statusBarStyle = .lightContent //(or .default) setNeedsStatusBarAppearanceUpdate() 

Key line of code: setNeedsStatusBarAppearanceUpdate()

0
Nov 29 '17 at 3:05
source share



All Articles