Hide swift 4 status bar

I am trying to hide the status bar in one of my UIViewControllers (Swift 4).

  • Firstly, I set the View state of the row based on the controller to YES in Info.plist .

  • I overridden the prefersStatusBarHidden property in my controller:


 override var prefersStatusBarHidden: Bool { return true } 

  • And in viewDidLoad() I added the setNeedsStatusBarAppearanceUpdate() function to make the prefersStatusBarHidden property read.

After all this, I still see the status bar on the UIViewController .

Can someone help me please?

+19
ios swift4
source share
16 answers

You probably already found your own solution, but I got it like this:

 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 } 
+7
source share

You can hide the status bar in any or all of your view controllers by simply adding this code:

 override var prefersStatusBarHidden: Bool { return true } 

Any view controller containing this code will by default hide the status bar.

If you want to animate the status bar to or from it, just call setNeedsStatusBarAppearanceUpdate () on your view controller - this will make prefersStatusBarHidden read again, on which you can return another value. If you want, your call to setNeedsStatusBarAppearanceUpdate () can actually be inside the animation block, which leads to the status bar hiding or showing smoothly.

+24
source share

If you represent a view controller modulo, try

 viewController.modalPresentationCapturesStatusBarAppearance = true 
+5
source share

Although some implementations are cleaner, such as:

UIApplication.shared.isStatusBarHidden = true

There are some weird clipping animations during transitions. Although more verbose, I prefer the @MachTurtle 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, works great for me.

+5
source share

Try checking the "View controller-based control panel status" check box in Info.plist to "YES." This causes the application to call the prefersStatusBarHidden: Bool property on each view controller.

View status bar status flag based on controller

+3
source share

Use the following code UIApplication.shared.isStatusBarHidden = true

This is the only thing I discovered in iOS11. you can write in didFinishLaunchingWithOptions or in 'viewWillAppear' of you BaseViewController Enjoy.

+1
source share

As you said, you are using the UINavigationController to navigate to your custom view controller. I assume you set your custom view controller as the root view of your UINavigationController. In this case, overriding var prefersStatusBarHidden in your custom view controller will not 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 } } 
+1
source share

When you try to overload the status bar properties for the ViewController, which is in UINavigationStack - you need to do the extension below

 extension UINavigationController { override open var childForStatusBarStyle: UIViewController? { return self.topViewController } } 

then your overloaded properties will become active

+1
source share

Try checking Hide status bar in the General section of your project settings.

Hide status bar in section

0
source share

None of them worked for me, working on a converted project in iOS 11. Here's what I did. I added this code to AppDelegate

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { application.isIdleTimerDisabled = true application.isStatusBarHidden = true return true } 
0
source share

Just change the “Top Space to” restriction to your view from the Safe Area to Superview. And it will drag your view under the status bar, so it will not need to be hidden ! [enter image description here ] 1

0
source share

I found that prefersStatusBarHidden not called in my view controller because I used a custom container view and I needed to send the status bar that hides responsibility to the child view controller. Implementation of var childForStatusBarHidden: UIViewController? { return childViewController } var childForStatusBarHidden: UIViewController? { return childViewController } var childForStatusBarHidden: UIViewController? { return childViewController } var childForStatusBarHidden: UIViewController? { return childViewController } in the container view controller, if for me.

0
source share

You must write code in the container view controller if it is a child view controller

  override var prefersStatusBarHidden: Bool { return true } 
0
source share

Add this to your info.plist

 <key>UIStatusBarHidden</key> <true/> 
0
source share

I was looking for this, and one job for me is

Swift 5

 override var prefersStatusBarHidden: Bool { return true } 
0
source share
 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView{ statusBar.isHidden = true } } 
-one
source share

All Articles