hide status bar swift 4

Viewed 23301

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
}

  • And in 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?

15 Answers

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.

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.

View controller-based status bar appearance flag

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.

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
    }

}

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.

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
 }

Try checking Hide Status Bar under the General section of your project's settings.

Hide status bar option under Project settings/General

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![enter image description here]1

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/>
Related