iOS how to make subview of a transparent view opaque?

The closest you're going to get is colorWithAlphaComponent:. Using something like the following, you can set the alpha component of the parent view's background, and it won't affect subviews.

[yourSuperview setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.5]];

If you set the parent view to 0.5 all the subviews will also go to 0.5 or less. You'll have to come up with a different design approach.


The following method worked for me.

superView.backgroundColor = .clear

Now that alpha property of superview is not altered, it won't affect the alpha of its subview. Hence, subviews will be opaque(if you have a background color for it).


For Swift 4.2

    self.yoursuperview.backgroundColor = UIColor.black.withAlphaComponent(0.5)

Tags:

Ios