hide status bar swift 4
You can hide the status bar in any or all of your view controllers just by adding this code:
override var prefersStatusBarHidden: Bool {
return true
}
Any view controller containing that code will hide the status bar by default.
If you want to animate the status bar in or out, just call setNeedsStatusBarAppearanceUpdate() on your view controller – that will force prefersStatusBarHidden to be read again, at which point you can return a different value. If you want, your call to setNeedsStatusBarAppearanceUpdate() can actually be inside an animation block, which causes the status bar to hide or show in a smooth way.
If you are presenting the view controller modally, try
viewController.modalPresentationCapturesStatusBarAppearance = true
You probably found your own solution to this already, but I got it working this way:
override func viewWillAppear(_ animated: Bool) {
// Sets the status bar to hidden when the view has finished appearing
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
// Sets the status bar to visible when the view is about to disappear
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar.isHidden = false
}