UIStatusBar style change + Xcode 10 + iOS 12
This article will help you to change the UIStatusBar style using Xcode 10 or later. Earlier we use, App.app().statusBarStyle = .default to change the appearance of UIStatusBar. From iOS 9.0 this is deprecated.
Now let us check how to change the UIStatusBar style.
Step 1:
Create a single view app, add one parameter “View controller-based status bar appearance” in Info.plist and set it to “NO”.
Step 2:
We have to override “preferredStatusBarStyle” and call “setNeedsStatusBarAppearanceUpdate” in order to change our status bar style.
eg: override var preferredStatusBarStyle: UIStatusBarStyle { return .default }
setNeedsStatusBarAppearanceUpdate()
Let’s try to change the color of the status bar with 2 button actions.
Define a var in our ViewController
var isDefault: Bool = false
Add 2 buttons named Black and White in your View Controller on in Storyboard and create corresponding IBActions for those.
@IBAction func black(_ sender: Any) {
isDefault = true
setNeedsStatusBarAppearanceUpdate()
}
@IBAction func white(_ sender: Any) {
isDefault = false
setNeedsStatusBarAppearanceUpdate()
}
That is it. Now the app will change status bar color with button actions.