Reputation: 33307
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?
Upvotes: 2
Views: 828
Reputation: 236410
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()
Upvotes: 3
Reputation: 264
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
}
}
Upvotes: 2
Reputation: 264
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?
Upvotes: 0