How to Change the Status Bar Style in iOS 12
February 16, 2019For some iOS apps, it may be helpful to change the color of the status bar at the top of the screen. For example, if I have a dark background, the default status bar style is hard to read:
To change the appearance of the status bar within a view controller, first add “View controller-based status bar appearance” as an item to your Info.plist
with a value of YES
:
Then in any view controller, you override the preferredStatusBarStyle
property:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
And if you ever need to update the status bar color, call setNeedsStatusBarAppearanceUpdate()
. Now the full view controller looks like this:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view,
// typically from a nib.
setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Running this view controller, we get a light status bar!