Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide status bar swift 4

Tags:

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?

like image 807
Dragisa Dragisic Avatar asked Oct 03 '17 11:10

Dragisa Dragisic


People also ask

How do I hide the notification bar in Swift?

Navigate to Android > Restrictions > Advanced and click on Configure. Under Display Settings, you'll have the following options. Hide System Bars – You can hide/display the system bars using this option.

How do I hide status bar in Swiftui?

Use statusBar(hidden:) to show or hide the status bar.

How do I hide the status bar in IOS 14?

To completely hide it on the home screen, open up an app, such as Settings, and wait about three to four seconds. Press the home button again, and the status bar will be completely gone.


2 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.

like image 161
Viraj Padsala Avatar answered Oct 02 '22 06:10

Viraj Padsala


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 } 
like image 21
MachTurtle Avatar answered Oct 02 '22 06:10

MachTurtle