2

I've got a generic UIViewController in which I would like to hide the status bar. I've got more view controllers which should display the status bar, but this specific view controller should hide the status bar.

I've implemented the following methods in the UIViewController class:

override func viewDidLoad() {
    super.viewDidLoad()
    // FIXME: hide status bar
    var prefersStatusBarHidden: Bool {
        return true
    }
    setNeedsStatusBarAppearanceUpdate()
}

override func viewWillAppear(_ animated: Bool) {
    UIApplication.shared.isStatusBarHidden = true
}

override func viewWillDisappear(_ animated: Bool) {
    UIApplication.shared.isStatusBarHidden = false
}

In my info.plist, I've set up the following setting:

enter image description here

The status bar does not hide when I navigate to that view controller and is still visible.

WalterBeiter
  • 2,021
  • 3
  • 23
  • 48

7 Answers7

8

override prefersStatusBarHidden in your view controller:

override var prefersStatusBarHidden: Bool {
    return true
}

Set a value No for View Controller based status bar appearance and then show/hide your status bar for specific view controller.

enter image description here

Here is result:

enter image description here

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • 1
    I had to implement the methods @pkc456 mentioned. And then set this setting to "NO". It is now working. Thanks. – WalterBeiter Mar 16 '18 at 11:34
  • using `prefersStatusBarHidden` in the `viewDidLoad` method did not work for me. The other methods work for me. – WalterBeiter Mar 16 '18 at 11:44
  • I did not handle anything in viewdidLoad, This is default method of UIViewController, you need to just override it and return a boolean value. There is result in this answer. – Krunal Mar 16 '18 at 11:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166953/discussion-between-walterbeiter-and-krunal). – WalterBeiter Mar 16 '18 at 11:49
1

In view controller where you want to hide the status bar,

In the viewWillAppear method, UIApplication.shared.isStatusBarHidden = true,

In the viewWillDisAppear method, UIApplication.shared.isStatusBarHidden = false

pkc456
  • 8,350
  • 38
  • 53
  • 109
  • edit: I changed the info.plist setting to "NO" and implemented your methods, the status bar is now gone, but it is black in all other view controllers – it was white before and shout be white. edit2: I edited the status bar style to "light" in the general tab, now it is working. – WalterBeiter Mar 16 '18 at 11:24
  • @WalterBeiter To change the color of status bar:- `UIApplication.shared.statusBarStyle = .lightContent` – pkc456 Mar 16 '18 at 11:25
  • 7
    `isStatusBarHidden` was deprecated in iOS 9 – Ashley Mills Oct 25 '18 at 13:07
1

To turn off the status bar for some view controllers but not all, remove this info.plist entry if it exists OR set it to YES:

View controller-based status bar appearance = YES

Then add this line to each view controller that needs the status bar hidden

override var prefersStatusBarHidden: Bool { return true } 

To turn off the status bar for the entire application, add this to info.plist:

View controller-based status bar appearance = NO

This will allow the "Hide status bar" to work as expected. Check the hide status bar located in the project's General settings under Deployment Info.

enter image description here

Marcy
  • 4,611
  • 2
  • 34
  • 52
0
UIApplication.shared.isStatusBarHidden = true

above this Setter for 'isStatusBarHidden' was deprecated in iOS 9.0

so use below code it's working fine :)

override var prefersStatusBarHidden: Bool {
        return true
    }
Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
Muthuraj M
  • 99
  • 1
  • 3
0

App Delegate swift 4.2

NotificationCenter.default.addObserver(self, selector: #selector(videoExitFullScreen), name:NSNotification.Name(rawValue: "UIWindowDidBecomeHiddenNotification") , object: nil)

@objc func videoExitFullScreen() {
        UIApplication.shared.setStatusBarHidden(false, with: .none)

    }
Srinivasan_iOS
  • 972
  • 10
  • 12
0

In Swift 5

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .default
}
Naishta
  • 11,885
  • 4
  • 72
  • 54
-2

Add following line in your ViewController

extension UIViewController {
    func prefersStatusBarHidden() -> Bool {
        return true
    }
}
snehal B.
  • 29
  • 6