I would like to change the status bar color between .lightContent
and .default
dynamically (since my background can change in the same ViewController).
Create a property with type UIStatusBarStyle
and return the value in preferredStatusBarStyle
And you can change its value whenever you need and call setNeedsStatusBarAppearanceUpdate()
class ViewController: UIViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return self.style
}
var style:UIStatusBarStyle = .default
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func changeStyle(_ sender: UIButton) {
if self.style == .lightContent {
self.style = .default
} else {
self.style = .lightContent
}
setNeedsStatusBarAppearanceUpdate()
}
}
override var preferredStatusBarStyle: UIStatusBarStyle { return self.style }
wont be called if you have embedded your view controller in a navigation controller You should change bar style of your navigation controller
self.navigationController?.navigationBar.barStyle = //newStyle