How can I have an opaque UIView as a subview of a semi-transparent UIView?

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)

No, not really. What you want is to take your overlay view, and make it just have a clear background color. As a subview of that new overlay place your view that will grey things out. And as a sibling view to that put your view you want to be opaque.

[OpaqueView] [DimmingView]
     |             |
      [OverlayView]

Don't put it inside the semi-transparent view. Make it a sibling to semi-transparent view and put it over it using z-ordering.


This will only work if you have any image on the background.

Instead of reducing the alpha of UIView, add an UIImageView on that view and then reduce the alpha of the UIImageView.

now add your subviews on the UIView.

your subviews will not take the alpha property anymore.. :)

Tags:

Ios

Iphone

Ipad