ios Swift中窗口视图顶部的子视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38540091/
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
Subview on top of window view in Swift
提问by Kex
I want to place a UIView over the entire screen (including the navigation bar). This view will be black with 0.3 opacity. I want to do this to darken out the screen content and push a view on top of this. I am using this code:
我想在整个屏幕(包括导航栏)上放置一个 UIView。此视图将为黑色,不透明度为 0.3。我想这样做是为了使屏幕内容变暗并在此之上推送一个视图。我正在使用此代码:
UIApplication.sharedApplication().keyWindow?.addSubview(darkView)
This covers the whole screen as expected. However I now want to place another view on top of this dark view. Is there a way to do this? Everything I try just results in the view being under the dark view. Any pointers would be really appreciated! thanks
这按预期覆盖了整个屏幕。但是我现在想在这个黑暗视图之上放置另一个视图。有没有办法做到这一点?我尝试的一切只会导致视图在黑暗视图下。任何指针将不胜感激!谢谢
回答by Sweeper
It's reallysimple.
这真的很简单。
You just add another view to window
! And it will be there, on top of the first view you added. For example, this code adds a black view and a white view:
您只需添加另一个视图window
!它将在您添加的第一个视图之上。例如,这段代码添加了一个黑色视图和一个白色视图:
let window = UIApplication.sharedApplication().keyWindow!
let v = UIView(frame: window.bounds)
window.addSubview(v);
v.backgroundColor = UIColor.blackColor()
let v2 = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))
v2.backgroundColor = UIColor.whiteColor()
window.addSubview(v2)
You can also add the new view as a sub view of the first view you added:
您还可以将新视图添加为您添加的第一个视图的子视图:
let window = UIApplication.sharedApplication().keyWindow!
let v = UIView(frame: window.bounds)
window.addSubview(v);
v.backgroundColor = UIColor.blackColor()
let v2 = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))
v2.backgroundColor = UIColor.whiteColor()
v.addSubview(v2)
Swift 4
斯威夫特 4
let window = UIApplication.shared.keyWindow!
let v = UIView(frame: window.bounds)
window.addSubview(v);
v.backgroundColor = UIColor.black
let v2 = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))
v2.backgroundColor = UIColor.white
v.addSubview(v2)
Simple!
简单的!
回答by Mario Jaramillo
For SWIFT 3 use this:
对于 SWIFT 3,请使用:
let window = UIApplication.shared.keyWindow!
window.addSubview(someView)
回答by Pramod More
Swift 4, Adding a UIViewControlleras a subview to UIWindow
Swift 4,将UIViewController作为子视图添加到UIWindow
This code is for adding view controller as a subview, covering the whole window with simple animation.
此代码用于将视图控制器添加为子视图,用简单的动画覆盖整个窗口。
let appDelegate = UIApplication.shared.delegate as! AppDelegate
var customReviewPopup = ReviewPopupViewController.init(nibName: "ReviewPopupViewController", bundle: Bundle.main)
self.appDelegate.window?.addSubview((customReviewPopup.view)!)
self.customReviewPopup.view.frame = (self.appDelegate.window?.bounds)!
self.customReviewPopup.view.alpha = 0
self.customReviewPopup.view.isHidden = true
UIView.animate(withDuration: 0.3, delay: 0, options: .transitionCrossDissolve, animations: {
self.customReviewPopup.view.isHidden = false
self.customReviewPopup.view.alpha = 1
}, completion: nil)