UIStatusBarStyle not working in Swift

前端 未结 11 490
轻奢々
轻奢々 2020-12-22 18:30

I\'m trying to change the Status Bar color in my Swift app to white, but am hitting a brick wall. I have 3 ViewControllers that are each embedded in a NavigationController (

相关标签:
11条回答
  • 2020-12-22 19:09

    Strange, using Swift 3.1 & XC8.2.1, but all of the above didn't work.

    What I did, is just

    extension UINavigationController
    {
        override open var preferredStatusBarStyle: UIStatusBarStyle {
            get {
                return .lightContent
            }
        }
    }
    

    No Plist, no other stuff. HTH

    0 讨论(0)
  • 2020-12-22 19:11

    In Swift 3.0 you can override a getter in ViewController for View controller-based status bar appearance:

    override var preferredStatusBarStyle: UIStatusBarStyle {
        get { return .lightContent }
    }
    
    0 讨论(0)
  • 2020-12-22 19:13

    for me all above dind't work until i add:

    self.navigationController?.navigationBar.barStyle = .black;
    

    so:

    1. Set UIViewControllerBasedStatusBarAppearance to YES in .plist
    2. In viewDidLoad call self.setNeedsStatusBarAppearanceUpdate();
    3. Override preferredStatusBarStyle
      override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
    4. In overrided method i set also the navigationBar.barStyle so final
      for light content:
      override var preferredStatusBarStyle: UIStatusBarStyle { self.navigationController?.navigationBar.barStyle = .black;//or default return .lightContent //or default }
      and for black content use default

    The source from here and here.

    and if this doesn't work you can try add a UINavigationController extension:

    extension UINavigationController
    {
        override open var preferredStatusBarStyle: UIStatusBarStyle {
            if let lastVC = self.viewControllers.last
            {
                return lastVC.preferredStatusBarStyle
            }
    
            return .default
        }
    }
    
    0 讨论(0)
  • 2020-12-22 19:16

    Don't edit your Info.plist. Add this to your ViewController.swift:

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.LightContent
    }
    
    0 讨论(0)
  • 2020-12-22 19:16

    Step 1. Add to info.plist View controller-based status bar appearance -> NO

    Step 2. Add code in method where you need to change status bar color:

    UIApplication.shared.statusBarStyle = .lightContent //(or .default)
    setNeedsStatusBarAppearanceUpdate()
    

    Key line of code: setNeedsStatusBarAppearanceUpdate()

    0 讨论(0)
  • 2020-12-22 19:24

    On iOS 9 the following (setStatusBarStyle) is deprecated and you will get a warning if you go that way.

    UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
    

    If you want all statusBars changed in a single shot try adding the following to your Info.plist. This will also make your launch-screen status bar white. While the code above won't.

    <key>UIStatusBarStyle</key>
    <string>UIStatusBarStyleLightContent</string>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    
    0 讨论(0)
提交回复
热议问题