How to change status bar style - iOS 12

梦想与她 提交于 2019-11-30 08:34:34

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
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!