当父视图的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)

最佳答案

不要设置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)

关于swift - 当父 View 的alpha值为0.5时,如何使UIView无法透视-SWIFT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50181444/

10-12 05:41