How to hide status bar of a single view controller in iOS 9?

后端 未结 15 1230
挽巷
挽巷 2020-12-13 06:15

In a ViewController, which I presented modally, I did this:

override func prefersStatusBarHidden() -> Bool {
    return true
}

This used

相关标签:
15条回答
  • 2020-12-13 06:33

    A late answer but if you need an alternative solution you can use this:

    public func ShowStatusBar() {
    
        let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow
        UIView.animate(withDuration: 0.3) {
            statusBarWindow?.alpha = 1
        }
    }
    
    public func HideStatusBar() {
    
        let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow
        UIView.animate(withDuration: 0.3) {
            statusBarWindow?.alpha = 0
        }
    }
    
    0 讨论(0)
  • 2020-12-13 06:34

    In iOS 9, Xcode 7, Swift 2.0, it's back to how it was previously.

    override func prefersStatusBarHidden() -> Bool {
            return true
    }
    

    In fact Xcode will tell you that

    UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: .None) 
    

    has been deprecated and that you should use the prefersStatusBarHidden method.

    0 讨论(0)
  • 2020-12-13 06:35

    You can achieve this by simply overriding prefersStatusBarHidden property in your ViewController as shown below :

    override var prefersStatusBarHidden: Bool {
       return true
    }
    

    This works for Swift 3/4.

    0 讨论(0)
  • 2020-12-13 06:37

    When all else fails (as it did for me)

    0 讨论(0)
  • 2020-12-13 06:42
    UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: .None) 
    

    and when you want it back in a separate VC:

    UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: .None) 
    
    0 讨论(0)
  • 2020-12-13 06:46

    For Swift 3 & Swift 4 it has changed to overriding a variable like this:

    override var prefersStatusBarHidden: Bool {
      return true
    }
    

    If you want to "Update" the state once the view controller is already being displayed, you will need to call:

    setNeedsStatusBarAppearanceUpdate()
    

    Please refer to the documentation.

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