I am trying to hide status bar in one of my UIViewControllers (Swift 4).
Firstly, I set View controller-based status bar appearance to
Use the following code
UIApplication.shared.isStatusBarHidden = true
this is the only thing that I found that is working in iOS11.
you can write in didFinishLaunchingWithOptions
or in 'viewWillAppear' of you BaseViewController
Enjoy.
Although some implementations are cleaner such as:
UIApplication.shared.isStatusBarHidden = true
There are some weird clipping animations during transitions. Although more verbose, I prefer @MachTurtle's solution:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
if let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as? UIView{
statusBar.isHidden = true
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar.isHidden = false
}
Try it out, works great for me.
I discovered that prefersStatusBarHidden
wasn't being called in my view controller because I was using a custom container view and I needed to forward the status bar hiding responsibility to the child view controller. Implementing var childForStatusBarHidden: UIViewController? { return childViewController }
in the container view controller fixed if for me.
Add this to your info.plist
<key>UIStatusBarHidden</key>
<true/>
I was searching for it and the one work for me is
Swift 5
override var prefersStatusBarHidden: Bool {
return true
}
Try setting "View controller-based status bar appearance" flag in Info.plist to YES. This will force app to call prefersStatusBarHidden: Bool
property on every view controller.