Store the status bar style as a property in your view controller:
var statusBarStyle: UIStatusBarStyle = .default
And then implement preferredStatusBarStyle
in the same view controller:
override var preferredStatusBarStyle: UIStatusBarStyle {
return statusBarStyle
}
Then when you change statusBarStyle
make sure to also call setNeedsStatusBarAppearanceUpdate
. The preferredStatusBarStyle
method is automatically called when the view appears/disappears, but if you change the status bar style while your view is visible, you have to tell the view controller the status bar appearance needs updating.
Note you still need to make the changes to the plist, and if your view controller is in a navigation controller, you may need to handle the status bar changes there instead (via a UINavigationController
subclass, for example).
UIApplication.shared.setStatusBarStyle(…)
was deprecated in iOS 9.0, so don't use that.