首先添加一个 UIView,甚至导航栏 [英] Add a UIView above all, even the navigation bar
问题描述
我想在任何其他视图(甚至导航栏)上方显示一种弹出"视图,如下所示:
I want to display, above any other views, even the navigation bar, a kind of "pop-up" view that looks like this:
- 带有 0.5 alpha 的全屏黑色背景,可以看到下面的其他
UIViewController
. - 中间有一个
UIView
窗口,其中包含一些信息(如果您想了解所有信息,可以使用日历).
- full screen black background with a 0.5 alpha to see the other
UIViewController
underneath. - a
UIView
window in the middle with some information, (a calendar if you want to know everything).
为此,我创建了一个包含两个 UIViews
(背景和窗口)的 UIViewController,并尝试显示它.我已经尝试了一个简单的 [mySuperVC addSubview:myPopUpVC.view]
,但我仍然有上面的导航栏.
To do that, I've created a UIViewController that contains the two UIViews
(background and window), and I'm trying to display it. I've tried a simple [mySuperVC addSubview:myPopUpVC.view]
, but I still have the navigation bar above.
我试图将它呈现为一个模态,但下面的 UIViewController
消失了,我失去了我的透明效果.
I've tried to present it as a modal, but the UIViewController
underneath disappears, and I lose my transparency effect.
任何想法都可以做到这一点,我相信这很简单......
Any idea to do this, I'm sure it's quite simple...
谢谢!
推荐答案
您可以通过将视图直接添加到 keyWindow 来实现:
You can do that by adding your view directly to the keyWindow:
UIView *myView = /* <- Your custom view */;
UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
[currentWindow addSubview:myView];
更新 -- 适用于 Swift 4.1 及更高版本
let currentWindow: UIWindow? = UIApplication.shared.keyWindow
currentWindow?.addSubview(myView)
iOS13 及以上更新
keyWindow
已弃用.您应该使用以下内容:
keyWindow
is deprecated. You should use the following:
UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.addSubview(myView)
这篇关于首先添加一个 UIView,甚至导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!