添加新 UIWindow 时以编程方式隐藏状态栏? [英] Programatically Hiding Status Bar When Adding New UIWindow?

查看:33
本文介绍了添加新 UIWindow 时以编程方式隐藏状态栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当显示我自己的自定义警报时,我目前正在向我的应用添加一个新的 UIWindow.

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

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

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.

这是我添加 UIWindow 的方式:

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];

推荐答案

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

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.

为了以编程方式执行此操作,您必须根据给定的偏好创建一个人造控制器,以便当您取消隐藏新的 UIWindow 时,状态栏不会在屏幕.

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
    }

}

并且实现看起来像:

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

这篇关于添加新 UIWindow 时以编程方式隐藏状态栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆