When developing iOS apps,Many applications need to set the color of the iOS system status bar according to the current App content color,Black or white-especially on iOS 13 After the system supports the black mode。
Normal,Others will tell you to do this:
1 |
UIApplication.shared.statusBarStyle = .default |
But this triggers a warning: Deprecated in iOS 9 。So is there any other way? Have,The official recommendation is this:
1 2 3 |
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } |
This is only in your ViewController ... but you find it in despair,It is not called at all。
So you continue to search ...
At your Info.plist in, View controller-based status bar appearance Must be set to YES ,Otherwise, you can only set the black and white color of the status bar in the Xcode target。
but…… preferredStatusBarStyle Still not called。
What exactly is going on?
This is actually due UINavigationController Lead,Because it is usually the highest level in ViewController,It won't preferredStatusBarStyle Request to pass down,opposite,It will be based on its own UINavigationBar.barStyle Properties to determine whether the system status bar is black or white,in case barStyle yes .black ,Then the system bar is .lightContent Which is white;but if barStyle yes .default ,Then the system bar is also the default black。
so,If you want to change the system bar color,exist UINavigationController ViewController under the need to be modified UINavigationBar.barStyle Instead of rewriting preferredStatusBarStyle 。
Other than that
If you are on iOS 13 Do this in iOS above,You will find that the system status bar remains the same ... so you may need to Info.plist Add UIUserInterfaceStyle Field,And set the value Light ,This can make your app unsuitable for iOS 13 Black mode-because if it fits,In black mode,Your system status bar will always be white,Does not change color ...
References
Original article written by LogStudio:R0uter's Blog » iOS correctly set the status bar style color
Reproduced Please keep the source and description link:https://www.logcg.com/archives/3343.html