let appDelegate = (UIApplication.sharedApplication().delegate! as! AppDelegate)
let backgrView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
backgrView.backgroundColor = UIColor.blackColor()
backgrView.alpha = 0.6
appDelegate.window!.addSubview(backgrView)

上面的代码允许我创建一个覆盖整个屏幕甚至导航控制器的视图
但是当我添加backgrView
下面的代码时,消失了:
let sampleview = UIView(frame: CGRectMake(0, 0, 10.0, 10.0))
sampleview.addSubview(backgrView)
如何将视图添加到backgrView
??
答案 0 :(得分:0)
即使是查看层次结构,也不会添加Sampleview,因此您可以向其添加backgrView。 试试这个:
let sampleview = UIView(frame: CGRectMake(0, 0, 10.0, 10.0))
backgrView.addSubview(sampleview)
或者这个:
let appDelegate = (UIApplication.sharedApplication().delegate! as! AppDelegate)
let backgrView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
backgrView.backgroundColor = UIColor.blackColor()
backgrView.alpha = 0.6
appDelegate.window!.rootViewController!.view.addSubview(backgrView)
let sampleview = UIView(frame: CGRectMake(0, 0, 10.0, 10.0))
backgrView.addSubview(sampleview)
答案 1 :(得分:0)
您的代码是倒退的:
sampleview.addSubview(backgrView)
你的意思是:
backgrView.addSubview(sampleview)