Swift UIView background color opacity
Setting alpha
property of a view affects its subviews. If you want just transparent background set view's backgroundColor
proprty to a color that has alpha component smaller than 1.
view.backgroundColor = UIColor.white.withAlphaComponent(0.5)
You can set background color of view to the UIColor with alpha, and not affect view.alpha
:
view.backgroundColor = UIColor(white: 1, alpha: 0.5)
or
view.backgroundColor = UIColor.red.withAlphaComponent(0.5)
You can also set it from InterfaceBuilder
by changing color's opacity:
For Swift 4.x and above
yourView.backgroundColor = UIColor.black.withAlphaComponent(0.5)