Swift UIView background color opacity Swift UIView background color opacity ios ios

Swift UIView background color opacity


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)


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)


For Swift 4.x and above

yourView.backgroundColor = UIColor.black.withAlphaComponent(0.5)

This works for me.

It may help you. Happy coding :)