当父视图的alpha值为0.5时,如何使UIView看不到 - SWIFT

时间:2018-05-04 18:50:10

标签: swift uiview transparency

当我的父视图的alpha值为0.5时,我试图让子视图无法透视。我的代码如下:

 // Background
 let popUpBackground = UIView.init(frame: self.view.frame)
 popUpBackground.backgroundColor = UIColor.lightGray
 popUpBackground.alpha = 0.5        

 // Popup
 var popUp = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
 popUp.backgroundColor = UIColor.blue
 popUp.alpha = 1.0 // This view appears to inherit the parents alpha value

 // Add popUp as subview to popUpBackground
 popUpBackground.addSubview(popUp)
 self.navigationController?.view.addSubview(popUpBackground)

2 个答案:

答案 0 :(得分:1)

您只需更改background color alpha

即可

UIColor.white.withAlphaComponent(alphaValue)

所以将popUpBackground代码更新为

 let popUpBackground = UIView.init(frame: self.view.frame)
    popUpBackground.backgroundColor = UIColor.white.withAlphaComponent(0.3)
   // popUpBackground.alpha = 0.1
   self.view.addSubview(popUpBackground)

答案 1 :(得分:0)

不要设置parentView的alpha,而是将alpha设置为parentView的背景颜色。

相反:

let popUpBackground = UIView.init(frame: self.view.frame)
popUpBackground.backgroundColor = UIColor.lightGray
popUpBackground.alpha = 0.5

使用:

let popUpBackground = UIView.init(frame: self.view.frame)
popUpBackground.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)