I am trying to hide status bar in one of my UIViewControllers (Swift 4).
Firstly, I set View controller-based status bar appearance to YES in Info.plist
.
I overrode the prefersStatusBarHidden
property in my controller:
override var prefersStatusBarHidden: Bool {
return true
}
viewDidLoad()
, I added setNeedsStatusBarAppearanceUpdate()
function to force the prefersStatusBarHidden
property to be read. After all that, I still see the status bar on that UIViewController
.
Can someone help me, please?
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.
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
}
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.
如果您以模态方式呈现视图控制器,请尝试
viewController.modalPresentationCapturesStatusBarAppearance = true
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.
As you said, you are using UINavigationController to navigate to your custom view controller. I suppose you have set your Custom View controller as the root view of your UINavigationController. In this case overriding var prefersStatusBarHidden in your custom view controller won't work but you will have to subclass your UINavigation Controller and override the property there as shown below:-
class CustomNavigationController: UINavigationController {
override var prefersStatusBarHidden: Bool {
return true
}
}
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
I was searching for it and the one work for me is
Swift 5
override var prefersStatusBarHidden: Bool {
return true
}
None of these worked for me either working on a converted project in iOS 11. Here's what I did. I added this code in the AppDelegate
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool
{
application.isIdleTimerDisabled = true
application.isStatusBarHidden = true
return true
}
Just change "Top Space to" constraint of your view from Safe area to Superview. And it will drag your view under the Status bar so there is will be no need to hide it ] 1
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 }
var childForStatusBarHidden: UIViewController? { return childViewController }
in the container view controller fixed if for me.
Need to write the code in the container view controller if it is a child view controller
override var prefersStatusBarHidden: Bool {
return true
}
Add this to your info.plist
<key>UIStatusBarHidden</key>
<true/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.