I\'m trying to change the Status Bar color in my Swift app to white, but am hitting a brick wall. I have 3 ViewControllers that are each embedded in a NavigationController (
Strange, using Swift 3.1 & XC8.2.1, but all of the above didn't work.
What I did, is just
extension UINavigationController
{
override open var preferredStatusBarStyle: UIStatusBarStyle {
get {
return .lightContent
}
}
}
No Plist, no other stuff. HTH
In Swift 3.0 you can override a getter in ViewController for View controller-based status bar appearance:
override var preferredStatusBarStyle: UIStatusBarStyle {
get { return .lightContent }
}
for me all above dind't work until i add:
self.navigationController?.navigationBar.barStyle = .black;
so:
UIViewControllerBasedStatusBarAppearance
to YES
in .plist
viewDidLoad
call self.setNeedsStatusBarAppearanceUpdate();
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
navigationBar.barStyle
so final
override var preferredStatusBarStyle: UIStatusBarStyle {
self.navigationController?.navigationBar.barStyle = .black;//or default
return .lightContent //or default
}
The source from here and here.
and if this doesn't work you can try add a UINavigationController
extension
:
extension UINavigationController
{
override open var preferredStatusBarStyle: UIStatusBarStyle {
if let lastVC = self.viewControllers.last
{
return lastVC.preferredStatusBarStyle
}
return .default
}
}
Don't edit your Info.plist. Add this to your ViewController.swift:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
Step 1. Add to info.plist View controller-based status bar appearance -> NO
Step 2. Add code in method where you need to change status bar color:
UIApplication.shared.statusBarStyle = .lightContent //(or .default)
setNeedsStatusBarAppearanceUpdate()
Key line of code: setNeedsStatusBarAppearanceUpdate()
On iOS 9 the following (setStatusBarStyle) is deprecated and you will get a warning if you go that way.
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
If you want all statusBars changed in a single shot try adding the following to your Info.plist. This will also make your launch-screen status bar white. While the code above won't.
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>