hide status bar swift 4

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?

Answers


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
}

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.


If you are presenting the view controller modally, try

viewController.modalPresentationCapturesStatusBarAppearance = true

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.


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.


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.


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
    }

}

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


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


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]1


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.


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

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

Swift 5

override var prefersStatusBarHidden: Bool {
  return true
 }

Need Your Help

How to use specified weights for fonts in XML

android android-xml android-styles android-fonts

Using the Fonts in XML feature you can specify various font weights for a font family. For example:

Do built-in types have default constructors?

c++ constructor value-initialization

After reading this article I made a point that int () yields 0 because the temporary int is value initialized and not because int() calls the default constructor for int. (The article is flawed acc...