I am trying to hide status bar in one of my UIViewControllers (Swift 4).
Firstly, I set View controller-based status bar appearance to
Need to write the code in the container view controller if it is a child view controller
override var prefersStatusBarHidden: Bool {
return true
}
Try checking Hide Status Bar under the General section of your project's settings.
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
}
When you try to overload statusbar properties for ViewController which in UINavigationStack - you need make extension below
extension UINavigationController {
override open var childForStatusBarStyle: UIViewController? {
return self.topViewController
}
}
then your overloaded properties will became active