21
votes

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
Maybe if you add some code it will be helpfull to make an answerKalamarico
@Anbu.Karthik The answer you've linked is something the OP has already done.Tamás Sengel
I have already tried all which is mention in @Anbu.Karthik commentDragisa Dragisic
Are you using some UIPageViewController or any other view hierarchy?PGDev

15 Answers

24
votes

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.

8
votes

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
}
5
votes

If you are presenting the view controller modally, try

viewController.modalPresentationCapturesStatusBarAppearance = true
4
votes

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

4
votes

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.

1
votes

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.

1
votes

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
    }

}
1
votes

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.

1
votes

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

1
votes

I was searching for it and the one work for me is

Swift 5

override var prefersStatusBarHidden: Bool {
  return true
 }
0
votes

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

Hide status bar option under Project settings/General

0
votes

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
}
0
votes

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

0
votes

Need to write the code in the container view controller if it is a child view controller

     override var prefersStatusBarHidden: Bool {
      return true
     }
0
votes

Add this to your info.plist

<key>UIStatusBarHidden</key>
    <true/>