How to change status bar style - iOS 12

前端 未结 6 1790
佛祖请我去吃肉
佛祖请我去吃肉 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:31

    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).

    0 讨论(0)
  • 2020-12-29 04:39

    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

    0 讨论(0)
  • 2020-12-29 04:39

    If you're wrapped in a nav controller, you're going to need this:

    final class LightNavigationController: UINavigationController {
        override var preferredStatusBarStyle: UIStatusBarStyle {
            return .lightContent
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-29 04:49

    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.

    0 讨论(0)
  • 2020-12-29 04:49
    override func viewDidLoad(){
        super.viewDidLoad()
    
        navigationController?.navigationBar.barStyle = .default
    }
    
    override var prefersStatusBarHidden: Bool {
        return true
    }
    

    I applied scroll view also in the same screen, by this code was able to resolve status bar issue.

    0 讨论(0)
提交回复
热议问题