0

I use the code:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return UIStatusBarStyle.lightContent
}

and I add

<key>UIViewControllerBasedStatusBarAppearance</key><false/>

in Info.plist.

But the StatusBar still is black style! Why?

Xiaohao
  • 3
  • 2
  • You done need to use Overrite method in Appdelegate : `didFinishLaunchingWithOptions` just put the code of line : UIApplication.shared.statusBarStyle = .lightContent – Nitin Gohel Mar 09 '17 at 12:55
  • @NitinGohel, please, do not suggest the code that's been deprecated. Setting the status bar style programmatically as you are suggesting has been deprecated on iOS 9.0. While it may work for the current version of iOS, in any future release, it may not. – Fahri Azimov Mar 09 '17 at 13:12
  • does it really matters :) @FahriAzimov then please post your answer for the help instead of wasting of your time :) – Nitin Gohel Mar 09 '17 at 13:15
  • Yes, it does matter. You should not teach the beginner to start development with deprecated methods. – Fahri Azimov Mar 09 '17 at 13:17

2 Answers2

4

SWIFT 3

UINavigationController overrides the view controller its preferred statusbar style. You can give control back to the view controller by subclassing the UINavigationController:

class BaseNavigationController: UINavigationController {

var statusBarStyle: UIStatusBarStyle?

override var preferredStatusBarStyle: UIStatusBarStyle {
    return statusBarStyle ?? .default
}

And then you're able to set the statusBarStyle property in the view controller:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if let navigationController = navigationController as? BaseNavigationController {
        navigationController.statusBarStyle = preferredStatusBarStyle
    }
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .default // any style you want
}
Lloyd Keijzer
  • 1,229
  • 1
  • 12
  • 22
2

It's not working because in Info.plist you have specified <key>UIViewControllerBasedStatusBarAppearance</key><false/>. Change it that key to true, and it'll work. Main idea behind that key is, when it's true, application looks in your view controllers code for the status bar style for the implementation of preferredStatusBarStyle (for status bar style) and prefersStatusBarHidden (for if it should hide status bar for this view controller). And, when the UIViewControllerBasedStatusBarAppearance key is false, application looks for the global settings defined in General section of the target preferences (choose project file in project navigator in xcode).

Also, you have to keep in mind that, when your view controller is on containers like UINavigationController or UITabbarController, in order to change the status bar appearance, you have to extends those containers (write extension), and override preferredStatusBarStyle property.

You can check this answer on SO as well.

Community
  • 1
  • 1
Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29