2016-02-09 44 views
0

我已经创建了导航栏的布局,并且我已将导航栏变为半透明。我已添加此代码:在导航栏missalignes视图上关闭半透明

var overlay : UIView? // This should be a class variable 

overlay = UIView(frame: view.frame) 
overlay!.backgroundColor = UIColor.blackColor() 
overlay!.alpha = 0.8 

view.addSubview(overlay!) 

如果我理解正确,这应该创建叠加在我的视图上。但它给了我这个结果: enter image description here 所以我认为这错过了我的观点。任何想法如何解决这个问题?

回答

1

它正在发生,因为如果您将半透明 设置为关闭,则查看原点会发生变化。因此,使用view.frame而不是使用view.bounds

var overlay : UIView? 

overlay = UIView(frame: view.bounds) 
overlay!.backgroundColor = UIColor.blackColor() 
overlay!.alpha = 0.8 

view.addSubview(overlay!) 
0

如下替换你的代码。

overlay = UIView(frame: view.bounds) 
overlay!.backgroundColor = UIColor.blackColor() 
overlay!.alpha = 0.8 

view.addSubview(overlay!) 

理由使用范围,而不是框架, 你已经把半透明的。所以你的观点将从(0,64)而不是(0,0)开始;

这就是为什么它获得y = 64帧值, 您可以设置y = 0或直接使用view.bounds,在边界它会给(x,y)=(0,0)和高度和宽度为与视图相同。