Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide status bar even when an alert is presented

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.

like image 520
Umair Afzal Avatar asked Sep 27 '16 07:09

Umair Afzal


People also ask

How to hide status bar in Android studio java?

View decorView = getWindow(). getDecorView(); // Hide the status bar. // status bar is hidden, so hide that too if necessary.

How do I hide the status bar on my Iphone?

Go to Your info. plist file. Add a key called “View controller-based status bar appearance” and set its value to NO.


5 Answers

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.

like image 136
Cyrille Avatar answered Oct 04 '22 02:10

Cyrille


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
        }
    }
}
like image 31
Majster Avatar answered Oct 04 '22 00:10

Majster


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.

like image 27
kashifasif Avatar answered Oct 04 '22 02:10

kashifasif


swift 2 version

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

swift 3 version

override var prefersStatusBarHidden: Bool {
    return true
}

To display alert:

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

like image 36
KSR Avatar answered Oct 04 '22 01:10

KSR


Just write an extension, do not create new class.

Swift 4

extension UIAlertController {
    open override var prefersStatusBarHidden: Bool {
        return true
    }
}
like image 37
えるまる Avatar answered Oct 04 '22 02:10

えるまる