views:

611

answers:

4

I have a somewhat transparent view (alpha = 0.6) that has some subviews. I'd like one of the subviews (a UILabel) to be drawn with alpha of 1.0 because the blending makes the text difficult to read but by adding it as a subview of the main view it has adopted its parent's alpha value. Is there a way to override this behavior? I believe I'll have to remove it from the subview but wanted to ask and see if maybe I'm missing something.

+1  A: 

You are correct. You'll have to move the UILabel out of the transparent view to get it to render as non-transparent.

Daniel
A: 

I don't think the previous answer was correct.

Views do not inherent the alpha of their parent view. You can set the alpha property independently for each view. For example, you can add a completely opaque view as a subview of a completely transparent one. You can also change alpha on the fly programatically.

Depending on the order of views and their alphas you might get unexpected blending effects but you can compensate by adjusting the ordering of your views.

TechZen
No, the previous answer is correct. If you change the alpha value on a view, that causes the remainder of the subviews to be composited onto the main view's superview at that opacity. Therefore, if you have a view with an alpha of 0.5 and a subview with an alpha of 0.5, that subview will appear to have an alpha of 0.25.
Brad Larson
A: 

Another approach which worked easily is to not modify the alpha channel on the UIView. Instead, modify the alpha layer on the backgroundColor object of the UIView.

Of course this depends on why you made the alpha layer transparent, but it worked well for my requirements.

Brian King
A: 

Brian is right. For example :

view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];

will make a Black background in alpha 0.6 and other views won't be affected.

Pierre Valade