How to change status bar style - iOS 12

前端 未结 6 1811
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 04:04

I need to update status bar style on every view controller based on the background color (what UINavigationController is doing automatically).

Have trie

6条回答
  •  孤独总比滥情好
    2020-12-29 04:41

    Swift 4+, 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 underneath it. 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.

提交回复
热议问题