In my info.plist
, file I have View controller-based status bar appearance
set to YES
I have a FirstViewController
where the status bar is hidden.
In my SecondViewController
I have
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override var prefersStatusBarHidden: Bool {
return false
}
override func viewDidLoad() {
super.viewDidLoad()
setNeedsStatusBarAppearanceUpdate()
}
However, the status bar is showing up, but is black.
How can I get it to update correctly? Thanks
Edit:
AppDelegate.swift
also has this
UIApplication.shared.statusBarStyle = .lightContent
in didFinishLaunchingWithOptions
Open your info. plist and set UIViewControllerBasedStatusBarAppearance to false . In the first function in AppDelegate. swift , which contains didFinishLaunchingWithOptions , set the color you want.
Build and Run the project to see the content of the status bar changed to light. Next, go back to the Storyboard, Select the View Controller and in the Editor menu Select Embed in Navigation Controller. Select the Navigation Bar and in the Attribute Inspector set the Bar Tint color to red.
You can change the status bar's color and material by inserting a small view right behind it. Normally, all views are positioned below the status bar, but you can use edgesIgnoringSafeArea(. top) to push it past the safe area insets.
There is a property in Info.plist
file called View controller-based status bar appearance. It should be set to YES.
Then in your UIViewController you should override preferredStatusBarStyle
:
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
Here is one important thing to notice: if you have view controllers embedded into UINavigationController and your view controller's preferredStatusBarStyle
method not getting called - you will have to workaround it by writing something like following:
extension UINavigationController {
override open var preferredStatusBarStyle : UIStatusBarStyle {
return topViewController?.preferredStatusBarStyle ?? .default
}
}
What it does is simply asking top controller for it's statusbar style, and update appropriately
There is a great deal of misunderstanding promulgated about how to govern the status bar style when your view controller is a child of a navigation controller.
Your child view controllers can implement preferredStatusBarStyle
, and this will work correctly if the navigation bar is hidden.
If the navigation bar is showing, the navigation controller sets the status bar style based on the navigation bar's barStyle
— to .default
if the bar style is .default
, and to .lightContent
if the bar style is .black
. So the correct way for your view controller to set the status bar style, when the navigation bar is showing, is to set the navigation controller's navigation bar style.
The obvious place to do this is in viewWillAppear
, which is called whenever this view controller becomes the top of the navigation controller's stack:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barStyle = .black // or .default
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With