Setting statusbarStyle (deprecated in iOS 9.0)

In swift4, You can use this block of code below viewDidLoad() in your ViewController-

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

If you use UINavigationController you also may want to use following code :

extension UINavigationController {
   open override var preferredStatusBarStyle: UIStatusBarStyle {
      return topViewController?.preferredStatusBarStyle ?? .default
   }
}

Reason is setNeedsStatusBarAppearanceUpdate() doesn't call childs preferredStatusBarStyle


Add View controller-based status bar appearance NO in Info.plist

And select Light in Status Bar Style in Deployment Info

enter image description here


Set your darkMode variable using the same code you have now, then use it in the computed variable that the system is expecting:

var darkMode = false
override var preferredStatusBarStyle : UIStatusBarStyle {
    return darkMode ? .default : .lightContent
}

Depending on the context you may need to force a refresh of the screen for it to take effect. You would do that with the following call:

setNeedsStatusBarAppearanceUpdate()

Tags:

Ios

Xcode

Swift