ios 首先添加一个 UIView,甚至是导航栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21850436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a UIView above all, even the navigation bar
提问by Nicolas Roy
I want to display, above any other views, even the navigation bar, a kind of "pop-up" view that looks like this:
我想在任何其他视图(甚至导航栏)上方显示一种“弹出”视图,如下所示:
- 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).
- 全屏黑色背景与 0.5 alpha 以查看
UIViewController
下面的另一个。 UIView
中间有一个窗口,里面有一些信息(如果你想知道一切,那就是日历)。
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.
为此,我创建了一个包含两者UIViews
(背景和窗口)的 UIViewController ,并尝试显示它。我试过一个简单的[mySuperVC addSubview:myPopUpVC.view]
,但我仍然有上面的导航栏。
I've tried to present it as a modal, but the UIViewController
underneath disappears, and I lose my transparency effect.
我试图将它呈现为一个模态,但UIViewController
下面的消失了,我失去了我的透明效果。
Any idea to do this, I'm sure it's quite simple...
任何这样做的想法,我相信这很简单......
Thanks!
谢谢!
回答by Rom.
You can do that by adding your view directly to the keyWindow:
您可以通过将视图直接添加到 keyWindow 来实现:
UIView *myView = /* <- Your custom view */;
UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
[currentWindow addSubview:myView];
UPDATE -- For Swift 4.1 and above
更新 - 对于 Swift 4.1 及更高版本
let currentWindow: UIWindow? = UIApplication.shared.keyWindow
currentWindow?.addSubview(myView)
回答by dalef
[self.navigationController.view addSubview:overlayView];
is what you really want
[self.navigationController.view addSubview:overlayView];
是你真正想要的
回答by Nam
Here is a simple elegant solution that is working for me. You can set the z position of the navigation bar to below the view:
这是一个对我有用的简单优雅的解决方案。您可以将导航栏的 z 位置设置为视图下方:
self.navigationController.navigationBar.layer.zPosition = -1;
Just remember to set it back to 0 when done.
只需记住在完成后将其设置回 0。
回答by Kevin ABRIOUX
Swift versions for the checked response :
已检查响应的 Swift 版本:
Swift 4 :
斯威夫特 4:
let view = UIView()
view.frame = UIApplication.shared.keyWindow!.frame
UIApplication.shared.keyWindow!.addSubview(view)
Swift 3.1 :
斯威夫特 3.1:
let view = UIView()
view.frame = UIApplication.sharedApplication().keyWindow!.frame
UIApplication.sharedApplication().keyWindow!.addSubview(view)
回答by Rashad
Add you view as the subview of NavigationController.
将您的视图添加为 NavigationController 的子视图。
[self.navigationController.navigationBar addSubview: overlayView)]
You can also add it over the window:
您还可以将其添加到窗口上:
UIView *view = /* Your custom view */;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:view];
Hope this helps.. :)
希望这可以帮助.. :)
回答by Allreadyhome
Dalef great solution in swift:
Dalef 快速解决方案:
self.navigationController?.view.addSubview(view)
回答by Hemang
@Nam's answer works great if you just want to display your custom view but if your custom view needs user interaction you need to disable interaction for the navigationBar
.
如果您只想显示自定义视图,@Nam 的回答非常有效,但如果您的自定义视图需要用户交互,则需要禁用navigationBar
.
self.navigationController.navigationBar.layer.zPosition = -1
self.navigationController.navigationBar.isUserInteractionEnabled = false
Like said in Nam's answer don't forget to reverse these changes:
就像 Nam 的回答中所说的那样,不要忘记扭转这些变化:
self.navigationController.navigationBar.layer.zPosition = 0
self.navigationController.navigationBar.isUserInteractionEnabled = true
您可以通过扩展以更好的方式执行此操作:
extension UINavigationBar {
func toggle() {
if self.layer.zPosition == -1 {
self.layer.zPosition = 0
self.isUserInteractionEnabled = true
} else {
self.layer.zPosition = -1
self.isUserInteractionEnabled = false
}
}
}
And simply use it like this:
只需像这样使用它:
self.navigationController.navigationBar.toggle()
回答by iOS Unit
Swift version of @Nicolas Bonnet 's answer:
@Nicolas Bonnet 的答案的 Swift 版本:
var popupWindow: UIWindow?
func showViewController(controller: UIViewController) {
self.popupWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
controller.view.frame = self.popupWindow!.bounds
self.popupWindow!.rootViewController = controller
self.popupWindow!.makeKeyAndVisible()
}
func viewControllerDidRemove() {
self.popupWindow?.removeFromSuperview()
self.popupWindow = nil
}
Don't forget that the window must be a strong property, because the original answer leads to an immediate deallocation of the window
不要忘记窗口必须是强属性,因为原始答案会导致窗口立即释放
回答by Nicolas Bonnet
I recommend you to create a new UIWindow:
我建议您创建一个新的 UIWindow:
UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.rootViewController = viewController;
window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
window.opaque = NO;
window.windowLevel = UIWindowLevelCFShareCircle;
window.backgroundColor = [UIColor clearColor];
[window makeKeyAndVisible];
Then you can manage your view in an other UIViewController. To remove the windows:
然后您可以在其他 UIViewController 中管理您的视图。要移除窗户:
[window removeFromSuperview];
window = nil;
hope that will help!
希望会有所帮助!
回答by Dani A
There is more than one way to do it:
有不止一种方法可以做到:
1- Add your UIView
on UIWindow
instead of adding it on UIViewController
. This way it will be on top of everything.
1- 添加你UIView
的UIWindow
而不是添加它UIViewController
。这样,它将位于一切之上。
[[(AppDelegate *)[UIApplication sharedApplication].delegate window] addSubview:view];
2- Use custom transition that will keep your UIViewController showing in the back with a 0.5 alpha
2- 使用自定义过渡,让您的 UIViewController 以 0.5 alpha 显示在后面
For that I recommend you look at this: https://github.com/Citrrus/BlurryModalSegue
为此,我建议你看看这个:https: //github.com/Citrrus/BlurryModalSegue