Ios – How to have an opaque UIView as a subview of a semi-transparent UIView

iosipadiphone

I have a UIView with an alpha of 0.5 which I add as a subview to my primary view in order to gray-out everything else. I want to add an additional UIView to this gray UIView as a subview – the problem is that when I do this, my newly-added subview is also partially transparent.

Is there any way to make a subview "ignore" the alpha value of its superview and be itself fully opaque?

Best Answer

Set the UIView background color alpha not it's alpha directly.

Objective-C

UIView *view;
...
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.6];

It's not the same as:

view.backgroundColor = [UIColor blackColor];    
view.alpha = .6;

Swift

view.backgroundColor = UIColor.black.withAlphaComponent(0.6)