How to hide status bar of a single view controller in iOS 9?
In a ViewController, which I presented modally, I did this:
override func prefersStatusBarHidden() -> Bool {
return true
}
This used to work, but it no longer works. What's the best way to hide the status bar only for this view controller?
Solution 1:
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.
Solution 2:
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
Solution 3:
In your UIViewController:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIApplication.shared.isStatusBarHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
//It will show the status bar again after dismiss
UIApplication.shared.isStatusBarHidden = false
}
override var prefersStatusBarHidden: Bool {
return true
}
Solution 4:
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.