Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically Hiding Status Bar When Adding New UIWindow?

I'm currently adding a new UIWindow to my app when displaying my own custom alert.

Before adding this UIWindow my app had the status bar hidden, but now it is visible. How can I hide the status bar programatically in this new window. I've tried everything but it doesn't work.

This is how I add my UIWindow:

notificationWindow = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, 1.0, 1.0)];

notificationWindow.backgroundColor = [UIColor clearColor]; // your color if needed
notificationWindow.userInteractionEnabled = NO; // if needed

// IMPORTANT PART!
notificationWindow.windowLevel = UIWindowLevelAlert + 1;

notificationWindow.rootViewController = [UIViewController new];
notificationWindow.hidden = NO; // This is also important!
[notificationWindow addSubview:confettiView];
like image 967
KingPolygon Avatar asked Apr 18 '14 09:04

KingPolygon


Video Answer


1 Answers

Essentially, since each UIWindow is independent of each other, you need to tell each one that you create what your preference is for the status bar.

In order to do this programmatically, you have to create a faux controller with your given preference so that when you unhide the new UIWindow, the status bar doesn't show/hide or flash on the screen.

class FauxRootController: UIViewController {

    // We effectively need a way of hiding the status bar for this new window
    // so we have to set a faux root controller with this preference
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

}

and the implementation would look like:

lazy private var overlayWindow: UIWindow = {
    let window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
    window.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    window.backgroundColor = UIColor.clearColor()
    window.windowLevel = UIWindowLevelStatusBar
    window.rootViewController = FauxRootController()
    return window
}()
like image 197
iwasrobbed Avatar answered Oct 14 '22 04:10

iwasrobbed