Setting statusbarStyle (deprecated in iOS 9.0)

后端 未结 6 1510
北恋
北恋 2020-12-13 09:13

Just downloaded the new xCode 10.0 and saw that the old statusBarStyle has been deprecated since iOS 9.0.

Warning: Setter for \'statusBarStyle\

相关标签:
6条回答
  • 2020-12-13 09:33

    Add View controller-based status bar appearance NO in Info.plist

    And select Light in Status Bar Style in Deployment Info

    0 讨论(0)
  • 2020-12-13 09:34

    My solution was as this: making an extension from the navigation controller:

    extension UINavigationController {
        open override var preferredStatusBarStyle: UIStatusBarStyle {
            if let topViewController = presentedViewController{
                return topViewController.preferredStatusBarStyle
            }
            if let topViewController = viewControllers.last {
                return topViewController.preferredStatusBarStyle
            }
    
            return .default
        }
    }
    

    and if you have a viewController that will have another style than the style of the app , you can make this

    var barStyle = UIStatusBarStyle.lightContent
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return barStyle
    }
    

    lets say that you app status style is .default and you want this screen to be .lightContent so barStyle will take the .lightContent as its default value, this will change the status bar style to lightContent, and then make sure when viewWillDisappear change the barStyle again to the app status bar style which in our case is .default .

    this is works for me

    0 讨论(0)
  • 2020-12-13 09:36

    None of the other suggestions worked for me. I ended up getting it to work by:

    1. Setting:

      override var preferredStatusBarStyle : UIStatusBarStyle {
          return .lightContent
      }
      
    2. Calling:

      setNeedsStatusBarAppearanceUpdate()
      
    0 讨论(0)
  • 2020-12-13 09:40

    Set your darkMode variable using the same code you have now, then use it in the computed variable that the system is expecting:

    var darkMode = false
    override var preferredStatusBarStyle : UIStatusBarStyle {
        return darkMode ? .default : .lightContent
    }
    

    Depending on the context you may need to force a refresh of the screen for it to take effect. You would do that with the following call:

    setNeedsStatusBarAppearanceUpdate()
    
    0 讨论(0)
  • 2020-12-13 09:43

    In swift4, You can use this block of code below viewDidLoad() in your ViewController-

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

    If you use UINavigationController you also may want to use following code :

    extension UINavigationController {
       open override var preferredStatusBarStyle: UIStatusBarStyle {
          return topViewController?.preferredStatusBarStyle ?? .default
       }
    }
    

    Reason is setNeedsStatusBarAppearanceUpdate() doesn't call childs preferredStatusBarStyle

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