I need to update status bar style on every view controller based on the background color (what UINavigationController
is doing automatically).
Have tried all the options described on stackoverflow (View controller-based status bar appearance
in info.plist
set to YES), but none worked for me.
I am using Xcode 10 beta 6 and Swift 4.2, targeting iOS 12.
Set View controller-based status bar appearance
to NO
in the info.plist
and override preferredStatusBarStyle
in each view controller like so:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
And call setNeedsStatusBarAppearanceUpdate()
in your view controller (in viewDidLoad()
for example).
Swift 4.2, iOS 12
View controller-based status bar appearance
now needs to be set to YES
in info.plist
since UIKit no longer wants us to edit status bar style through UIApplication.shared
--status bar style is now view-controller based.
Then if you want the change to be applied at app level, simply override preferredStatusBarStyle
in the appropriate container view controller (ideally the root)...
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
...and this will propagate to all view controllers. And if you want to edit status bar style per view controller, apply this override per view controller.
If status bar style ever changes during runtime, then you need to call setNeedsStatusBarAppearanceUpdate()
(from anywhere in the container/root view controller or that specific view controller), otherwise it isn't needed.
If you have View controller-based status bar appearance
in info.plist
set to YES and your view controller is embedded into UINavigationController
, then your navigation controller will be responsible for updating bar style (through navigationController.navigationBar.barStyle
) and preferredStatusBarStyle
property will be ignored
I have Xcode 10.2 and found as you did that setting the View controller-based status bar appearance in info.plist to "YES" will not change the status bar style at all.
However, I did find that changing two keys in the info.plist file will do all the work of changing the status bar to light or dark without any additional coding.
Here's what I did to fix it for myself
When hovering over the top line "Information Property List" from the info.plist file, a small round "+" button will appear. Click this button and scroll through the items until you find the following keys.
View controller-based status bar appearance [set this value to] NO
Status bar style [set this value to] UIStatusBarStyleLightContent
NOTE: The UIStatusBarStyleLightContent is not found as a selectable item in the value list and must be typed into the value box.
I hope this helps you or anyone else looking for an answer to this question.
Finally, what worked for me:
Add to Info.plist:
View controller-based status bar appearance : YES
For each ViewController add to viewDidLoad() and viewDidAppear():
self.setNeedsStatusBarAppearanceUpdate()
a) For dark color status bar, add to ViewController:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .default
}
b) For white color status bar, add to ViewController:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
If you're wrapped in a nav controller, you're going to need this:
final class LightNavigationController: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
来源:https://stackoverflow.com/questions/52238121/how-to-change-status-bar-style-ios-12