preferredStatusBarStyle var not working in iOS12?

Nostradamus picture Nostradamus · Sep 22, 2018 · Viewed 13.8k times · Source

Adding

application.statusBarStyle = .lightContent

to my AppDelegate's didFinishLaunchingWithOptions method nor adding

override var preferredStatusBarStyle: UIStatusBarStyle {
    return UIStatusBarStyle.lightContent
}

to the VC no longer works on iOS12/Xcode10

Any ideas?

Answer

matt picture matt · Sep 22, 2018

This has nothing to do with iOS 12. You just have the rules wrong.

In a navigation controller situation, the color of the status bar is not determined by the view controller’s preferredStatusBarStyle.

It is determined, amazingly, by the navigation bar’s barStyle. To get light status bar text, say (in your view controller):

self.navigationController?.navigationBar.barStyle = .black

Hard to believe, but true. I got this info directly from Apple, years ago.

You can also perform this setting in the storyboard.

Example! Navigation bar's bar style is .default:

enter image description here

Navigation bar's bar style is .black:

enter image description here

NOTE for iOS 13 This still works in iOS 13 as long as you don't use large titles or UIBarAppearance. But basically you are supposed to stop doing this and let the status bar color be automatic with respect to the user's choice of light or dark mode.