添加新的UIWindow时以编程方式隐藏状态栏?

Kin*_*gon 4 objective-c statusbar uiviewcontroller uiwindow

我正在显示自己的自定义提醒时,我正在向我的应用添加新的UIWindow.

在添加此UIWindow之前,我的应用程序隐藏了状态栏,但现在它可见.如何在这个新窗口中以编程方式隐藏状态栏.我已经尝试了一切,但它不起作用.

这就是我添加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];
Run Code Online (Sandbox Code Playgroud)

KNa*_*ito 5

notificationWindow有一个rootViewController.因此,使用preferStatusBarHidden方法将Custom UIViewController实现为rootViewController

- (BOOL)prefersStatusBarHidden {
  return YES;
}
Run Code Online (Sandbox Code Playgroud)

使用默认UIViewController实例的instread [UIViewController new].


iwa*_*bed 5

本质上,由于每个人UIWindow都是彼此独立的,因此您需要告诉每个人您创建的状态栏首选项是什么。

为了以编程方式执行此操作,必须创建具有给定首选项的仿控制器,以便在取消隐藏新控制器时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
    }

}
Run Code Online (Sandbox Code Playgroud)

和实现看起来像:

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
}()
Run Code Online (Sandbox Code Playgroud)