I am hiding my status bar on a specific viewController
using
override func prefersStatusBarHidden() -> Bool {
return true
}
It is working very nice until I present an alert on the screen. When alert is presented status bar appears again, Which I do not want. When alert is dismissed status bar is hidden again.
View decorView = getWindow(). getDecorView(); // Hide the status bar. // status bar is hidden, so hide that too if necessary.
Go to Your info. plist file. Add a key called “View controller-based status bar appearance” and set its value to NO.
As UIAlertController
is now a full-fledged UIViewController
, you should be able to subclass it and add the same method to the new subclass. Then instanciate your subclass instead of a plain UIAlertController
.
Untested, but that should do the trick.
It's not the prettiest solution but since UIAlertController
is now just a subclass of UIViewController
you can subclass it and override the prefersStatusBarHidden
just as you did with your other view controllers.
Everything else stays the same.
Swift3:
final class MYAlertController : UIAlertController {
override var prefersStatusBarHidden: Bool {
get {
return true
}
}
}
Create a class named as CustomAlertController and inherit it from UIAlertController
write this method in this class
override func prefersStatusBarHidden() -> Bool {
return true
}
and whenever you create an alert, create an instance of CustomAlertController and then use it.
override func prefersStatusBarHidden() -> Bool {
return true
}
override var prefersStatusBarHidden: Bool {
return true
}
let alertController = UIAlertController(title: "Error", message: "No internet connection", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction) in
print("OK button pressed");
}
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion:nil)
}
Please check this link to test:
https://github.com/k-sathireddy/AlertControllerSample
Just write an extension, do not create new class.
Swift 4
extension UIAlertController {
open override var prefersStatusBarHidden: Bool {
return true
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With