6

Hi everyone I'm trying to hide my statusBar in a View Controller but it doesn't seem to work .. I used the function:


 override var prefersStatusBarHidden: Bool {
         return true
    }

I also set the View controller-based status bar appearance in the plist file to YES

My status bar doesn't want to hide ... where am I doing wrong?

kAiN
  • 2,559
  • 1
  • 26
  • 54
  • `View controller-based status bar appearance` to `NO` under `Info.plist`. You can check your settings for status bar [here](https://i.stack.imgur.com/qK9c2.png) – Vadim Nikolaev Jan 14 '20 at 12:00
  • try this : `(UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow)?.isHidden = false` – Keshu R. Jan 14 '20 at 12:27
  • crash with this error UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.' – kAiN Jan 14 '20 at 12:37
  • @kAiN did you check my answer? I've tested right now with empty project with `Status bar is initially hidden` as `YES` and `View controller-based status bar appearance` as `NO` in `plist` without adding any code - the status bar is hidden on iOS13.3 and iPhone11 – Vadim Nikolaev Jan 14 '20 at 13:04
  • @VadimNikolaev Yes, I know that, but I need my status bar not only visible in a specific view controller – kAiN Jan 14 '20 at 13:07
  • @kAiN please check [this solution](https://stackoverflow.com/a/52210933/5928311). I guess it'll useful for your case – Vadim Nikolaev Jan 14 '20 at 13:47

3 Answers3

10

It looks like you're trying to specifically hide the status bar in a single ViewController.

In order to do that, you need have the following in that ViewController

self.modalPresentationCapturesStatusBarAppearance = true

override var prefersStatusBarHidden: Bool {
      return true
}

I also added View controller-based status bar appearance in my .plist and set it to YES.

Tested on the latest iOS 13.

bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
Michael
  • 193
  • 2
  • 12
  • It works for me. But on iOS 14, I do not need to set `self.modalPresentationCapturesStatusBarAppearance = true` , it also works well. – Daemonson Dong Oct 16 '20 at 02:48
0

If the target view controller is embedded in another container view controller such as UINavigationController you need to subclass that container view controller and override its childForStatusBarHidden to return the target view controller.

an0
  • 17,191
  • 12
  • 86
  • 136
0

you kan look this,you should override childForStatusBarHidden and childForStatusBarStyle 。

class CCNavigationController: UINavigationController {
    override var childForStatusBarHidden: UIViewController? {
        return self.topViewController
    }
    
    override var childForStatusBarStyle: UIViewController? {
        return self.topViewController
    }
}

1215ccc
  • 41
  • 5