UIStatusBarStyle not working in Swift

61,098

Solution 1

You have two options.

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

View controller-based status bar appearance

Or, if you want to continue to use view controller based status bar appearance, instead of setting the application's statusBarStyle, override the preferredStatusBarStyle property in each view controller for which you'd like to specify a status bar style.

Swift 3

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

Swift 2

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}

Solution 2

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

Solution 3

You have to set the:

navigationController.navigationBar.barStyle = .black

and the text will appear in white

Solution 4

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's style to .Lightcontent for all the view controllers inside a UINavigationController.

(Also, delete View controller-based status bar appearance from Info.plist to suppress the warnings you're probably seeing too!)

Solution 5

In Swift 3, status bar style has changed to a computed property in UIViewController that you can override like this:

override var preferredStatusBarStyle: UIStatusBarStyle {
   return .lightContent //or default
} 
Share:
61,098

Related videos on Youtube

davidrayowens
Author by

davidrayowens

Missionary and graphic designer.

Updated on November 29, 2020

Comments

  • davidrayowens
    davidrayowens over 3 years

    I'm trying to change the Status Bar color in my Swift app to white, but am hitting a brick wall. I have 3 ViewControllers that are each embedded in a NavigationController (could that be the issue? I've already tried to place the code in the NavigationController class.) I've tried both of the following pieces of code in the didFinishLaunchingWithOptions of my AppDelegate.swift file but neither worked.

    application.statusBarStyle = .LightContent
    

    and

    UIApplication.sharedApplication().statusBarStyle = .LightContent
    

    All that the Docs have to say about it is that UIBarButtonStyle is an Int and gave me this enum snippet which didn't help me at all with implimentation.

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

    What am I missing?

  • Rahul
    Rahul about 9 years
    I have tried this code... but I only get the white status bar in first screen.. I am not getting the status bar for the rest of the screens :(.. any help?
  • StackUnderflow
    StackUnderflow about 9 years
    You have to set the "View controller-based status bar appearance" setting in your plist to YES.
  • Sam Holmes
    Sam Holmes almost 9 years
    That's only if you want to use a view controller based status bar appearance.
  • 1b0t
    1b0t over 8 years
    thank you. There seems to be a serious bug in iOS9. This was the only way I could turn the status bar in the initial VC white. I tried everything. I'll file an radar.
  • Pandey_Laxman
    Pandey_Laxman over 8 years
    preferredStatusBarStyle returning UIStatusBarStyleDefault but not updating lightcontent to darkcontent for specific viewcontroller . After self.navigationController.navigationBar.barStyle=UIBarStyleD‌​efault; in viewWillAppear worked for me . Hope this comment will help others.
  • Svitlana
    Svitlana over 8 years
    Thanks! There was nothing helped even this override func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.LightContent } this really helps: navigationController.navigationBar.barStyle = .BlackTranslucent navigationController.navigationBar.translucent = true
  • hsusmita
    hsusmita over 8 years
    Thank! You saved my day. However, BlackTranslucent is deprecated. Hence, I used navigationController.navigationBar.barStyle = .Black
  • Jay Mayu
    Jay Mayu about 8 years
    This is the perfect answer :)
  • Mehul
    Mehul about 8 years
    Perfect answer. Short and sweet.
  • auco
    auco almost 8 years
    sorry, this might be the simplest answer, but it's definitely not the BEST: because it just sets this one style to all and any viewControllers in the app. The behaviour has been introduced to enable finer control and apply different settings based on different view controllers (e.g. mixing light and dark contents).
  • Bill Chan
    Bill Chan almost 8 years
    The .plist approach is correct, but setStatusBarStyle() is not necessary. 'setStatusBarStyle(_:animated:)' was deprecated in iOS 9.0: Use -[UIViewController preferredStatusBarStyle]
  • GuiSoySauce
    GuiSoySauce almost 8 years
    @BillChan thanks for commenting but please read the answer gain. I already explained 'setStatusBarStyle' was deprecated. You should use preferredStatusBarStyle if you want one or another viewControllers changed. The plist approach is for who wants all viewControllers changed in a single shot. There is no right or wrong here. Just a matter of what you need to achieve.
  • stevo.mit
    stevo.mit over 7 years
    in iOS 10 (Swift 3.0) its no longer method but property. See: stackoverflow.com/questions/38862208/…
  • Despotovic
    Despotovic over 7 years
    No comment for navigation bar style... but in Swift 3, Xcode 8 - this was the only solution that worked for me.
  • n13
    n13 over 7 years
    It's bizarre - not what the Apple docs say, but it works. Tried a bunch of other ways, including have a UIViewController based status bar, none of them worked.
  • Chris Allinson
    Chris Allinson over 7 years
    UIStatusBarStyle = UIStatusBarStyleLightContent (in .plist) helped my app launch with white status bar text ... Thanks!!!
  • Lukasz 'Severiaan' Grela
    Lukasz 'Severiaan' Grela over 7 years
    The only drawback I've noticed is that with all those steps it changes but without animation
  • Oubaida AlQuraan
    Oubaida AlQuraan over 7 years
    just for one screen
  • brycejl
    brycejl almost 7 years
    Note that the UIStatusBarStyle has been changed to ".black" (lowercase B) in Swift 3
  • Bhavin_m
    Bhavin_m almost 7 years
    It's perfectly working with override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }. You just have to write this line in viewDidLoad for wrok well. Make sure keep View controller-based status bar appearance = YES with this solution.
  • zeeshan
    zeeshan over 5 years
    Works for Swift 4.2 too.
  • raistlin
    raistlin almost 5 years
    seems like an overkill... just tried it and worked like this in Info.plist file: 1. Status bar style -> UIStatusBarStyleLightContent 2. View controller-based status bar appearance -> NO
  • Leon
    Leon over 3 years
    Making this change made my back button disappear.