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

后端 未结 15 1231
挽巷
挽巷 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:54

    For Swift 3,

    override var prefersStatusBarHidden: Bool{
            return true
        }
    

    and add viewDidLoad()

    self.modalPresentationCapturesStatusBarAppearance = true
    
    0 讨论(0)
  • 2020-12-13 06:54

    I'm using Xcode Version 9.2 / Swift 3.2 / iOS 11

    I got this answer form BADCloud although I'm not sure why it didn't work for him.

    Anyway it worked for me for a specific view controller.

    The difference between this answer and the other answers on here is that in my info.plist when I initially had View controller-based status bar appearance - NO with the 2 statusBar methods below it didn't work but when I changed it to Yes is worked with both of them.

    In the info.plist change: View controller-based status bar appearance - YES

    In the view controller you want it changed in add:

    override func viewDidLoad() {
            super.viewDidLoad()
    
            // add this in ViewDidLoad
            setNeedsStatusBarAppearanceUpdate()
    }
    
    // add this underneath ViewDidLoad
    override var prefersStatusBarHidden: Bool {
      return true
    }
    
    0 讨论(0)
  • 2020-12-13 06:55

    For Swift 3 and Swift 4.2 when view going to Appear

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        UIApplication.shared.isStatusBarHidden = true
    }
    

    when view goint to Dissapear

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    
        UIApplication.shared.isStatusBarHidden = false
    }
    

    It's possible you need set in your info.plist, next line:

    View controller-based status bar appearance = NO
    

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