Programmatically hide status bar when adding new UIWindow?

I am currently adding a new UIWindow to my application when I show my own custom warning.

Before adding this UIWindow, my application hid the status bar, but now it is visible. How can I hide the status bar programmatically in this new window. I tried everything, but it does not 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];
+4
source share
4 answers

Your notificationWindowhave rootViewController. Therefore, implement the custom UIViewController as rootViewController using the preferStatusBarHidden method as

- (BOOL)prefersStatusBarHidden {
  return YES;
}

UIViewController [UIViewController new].

+5

, UIWindow , , , .

, faux- , UIWindow / .

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
    }

}

:

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
}()
+4

Swift 4.2

ViewController:

class NoStatusBarViewController: UIViewController {    

    override var prefersStatusBarHidden: Bool {

        return true

    }

}

UIWindow:

rootViewController = NoStatusBarViewController()
0

UIAlertController UIAlertView.

notificationWindow.windowLevel = UIWindowLevelNormal;
-1

Source: https://habr.com/ru/post/1537071/


All Articles