According to the documentation for UIVIew @property(nonatomic) CGFloat alpha
The value of this property is a floating-point number in the
For now there is only one way make the Parent View transparent and don't put any child views inside (don't put any views as subview) the parent view, put that child views outside of the parent view. To make parent view transparent you can do this via storyboard.
//Transparent the parentView
parentView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)
Put the other view outside of the parent view. It will work like a charm.
Please refer to the bold description from Xcode documentation.
The value of this property is a floating-point number in the range 0.0 to 1.0, where 0.0 represents totally transparent and 1.0 represents totally opaque. Changing the value of this property updates the alpha value of the current view only. However, the transparency imparted by that alpha value affects all of the view's contents, including its subviews. For example, a subview with an alpha value of 1.0 that is embedded in a parent view with an alpha value of 0.5, appears onscreen as if its alpha value is also 0.5.
Set Opacity of the background color instead of alpha will not affect its child views.
Or you can set by programmetically
var customView:UIView = UIView()
customView.layer.opacity = 0.3
Thats it. Happy Coding!!!
In Swift 4.2 and Xcode 10.1
Don't add colour and alpha value through storyboard. Only programmatic approach will work in this case.
transparentView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
I think this is a bug in the documentation. You should file it at bugreport.apple.com.
Everything I can see after a bit of quick research suggests what you are seeing is how it always has behaved, and my own testing shows it too.
The alpha of a view is applied to all subviews.
Perhaps all you need is [[UIColor blackColor] colorWithAlphaComponent:0.5]
but if not you will need to make the view a sibling instead of a child.
If you like Storyboards, put a User Defined Runtime Attribute
for your view in the Identity Inspector
:
Key Path: backgroundColor
, Type: Color
, Value:
e.g. white color with Opacity 50 %.