2

I want to create a global function which I can use to show and hide the status bar. Here is what I did:

class Helper {
      class func hide() {
        let app = UIApplication.sharedApplication()
        if !app.statusBarHidden {
          app.statusBarHidden = true
        }
      }
      class func show() {
        let app = UIApplication.sharedApplication()
        if app.statusBarHidden {
          app.statusBarHidden = false
        }
      }
}

Here is how it is called:

Helper.hide()

I put these functions in a helper class. Calling the hide() function does not hide the statusbar.

I also set in info.plist Status bar is initially hidden

How can I show and hide StatusBar from a global function?

Michael
  • 32,527
  • 49
  • 210
  • 370

3 Answers3

3

create a static boolean property on UIViewController called isStatusBarHidden.

extension UIViewController {
    static var isStatusBarHidden = false
}

At the desired view controllers you just need to override prefersStatusBarHidden property

class ViewController: UIViewController {
    // ..
    override var prefersStatusBarHidden: Bool {
        return UIViewController.isStatusBarHidden
    }
    // ..
}

Then all you need to do is set your var to true or false as desired:

UIViewController.isStatusBarHidden = true

To toggle on / off the status bar of the view controllers that you override that property:

UIViewController.isStatusBarHidden = !UIApplication.shared.isStatusBarHidden

don't forget to call setNeedsStatusBarAppearanceUpdate

setNeedsStatusBarAppearanceUpdate()
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
2

The setStatusBarStyle in appDelegate is not forwarded to the view controllers. They control their own state. So try this instead:

Create a baseViewController class derived from UIViewController which implements the functions hide and show. Then derive the view controllers that you are using from the baseViewController class.

class baseViewController : UIViewController {

    var statusBarHidden : Bool = true

    func show()
    {
        self.statusBarHidden = false
        setNeedsStatusBarAppearanceUpdate()
    }

    func hide()
    {
        self.statusBarHidden = true
        setNeedsStatusBarAppearanceUpdate()
    }

    override func prefersStatusBarHidden() -> Bool {
        return self.statusBarHidden
    }
}

class derivedViewController: baseViewController {
    override func viewDidLoad() {
        hide() // run this to hide the status bar
        show() // run this to show the status bar
    }
}
Ruud Kalis
  • 264
  • 3
  • 9
0

In your hide function the app variable is unknown. When I run your example I had to change to:

func hide() {
    let app = UIApplication.sharedApplication()
    if !app.statusBarHidden {
        app.statusBarHidden = true
    }
}
func show() {
    let app = UIApplication.sharedApplication()
    if app.statusBarHidden {
        app.statusBarHidden = false
    }
}

If this not works with a global function you could try adding this to your ViewControllers:

    override func prefersStatusBarHidden() -> Bool {
    return true
}

Also notice that in your info.plist file, there's a setting called "Status bar is initially hidden." Set that to "YES," and you won't have it at startup. Then you don't need to do anything in your code, the bar will show up when your app is launched. See previous discussion:

How to set status bar hidden for entire app in iOS?

Community
  • 1
  • 1
Ruud Kalis
  • 264
  • 3
  • 9
  • Thats not what the user wants to do. He wants the option to show and hide status bar, not have it hidden throughout app – JSA986 Apr 23 '15 at 13:53
  • Just found this in an [older page:] (http://stackoverflow.com/questions/19022210/preferredstatusbarstyle-isnt-called)The UINavigationController does not forward on preferredStatusBarStyle calls to its child view controllers. Instead it manages its own state - as it should, it is drawing at the top of the screen where the status bar lives and so should be responsible for it. Therefor implementing preferredStatusBarStyle in your VCs within a nav controller will do nothing - they will never be called. – Ruud Kalis Apr 23 '15 at 14:13
  • After an intense search I concluded that using a helper class in this way will probably not work. Try the alternative that I present. That works well for me and will probably just require changes to your solution. – Ruud Kalis Apr 23 '15 at 15:12
  • apple removed UIStatusBarHidden~ipad from Xcode 7, iOS9 this solution is the only one that works globally. +1 – μολὼν.λαβέ Jul 09 '15 at 21:48