hide status bar swift 4

Dragisa Dragisic picture Dragisa Dragisic · Oct 3, 2017 · Viewed 22.3k times · Source

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?

Answer

Viraj Padsala picture Viraj Padsala · Oct 3, 2017

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.