Setting Alpha on Uiview Sets the Alpha on Its Subviews Which Should Not Happen

Setting alpha on UIView sets the alpha on its subviews which should not happen

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.

Can you override a parent UIView's alpha value on one of its subviews?

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

Why do all the subviews of a UIView show translucency when the UIView's alpha is set less than 1.0?

Here comes from Apple's View and Window Architecture documentation:

In addition to providing its own content, a view can act as a
container for other views. When one view contains another, a
parent-child relationship is created between the two views. The child
view in the relationship is known as the subview and the parent view
is known as the superview. The creation of this type of relationship
has implications for both the visual appearance of your application
and the application’s behavior.

So when you set dimView.alpha = 0.4;(which is your super view) it automatically changes the opacity of it's sub views too. Because your super view holds those sub views and underlying layers which drawn by Core Animation.

If you want to change opacity of your super view only, you can do this via:

[dimView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]]; 

See also:

  • Creating and Managing a View Hierarchy

Subview has higher alpha then parent?

You can't make the alpha of a child view appear higher (less transparent) than its parent view.

A child view's effective alpha is determined by its own alpha times the alpha of its parent view.

What you can do it put both views into a common container view. Make the container view have a clear background color. Then the two views can each have their own alpha.

iPhone Programming: Applying Alpha to Parent but not to Child Views

The parent that acts as your background should be changed to a sibling before the container that wraps your children. That way you can set the transparency without affecting the entire hierarchy.

This would look like this.

  • <item-container>
    • <item-background>
    • <children-container>
      • <children/>

This hierarchy, would have the same visibility order and let you set the opacity independently of the contents of the children container.

UIView opaque property

The answer is that iOS is smart enough to recognize that if your view's alpha is less than 1, it needs to draw the content behind your view, regardless of your view's opaque property.

In response to comments: from my limited experimentation, I don't think the view's opaque property has any effect. (I think the documentation is wrong.) The view's layer's opaque property does have an effect: it controls whether the CGContext passed to drawRect: has an alpha channel. If the layer's opaque property is YES, the context has no alpha channel (and is treated as if every pixel has alpha of 1.0).

Changing the view's opaque property has no effect on the layer's opaque property. This is different than (for example) the view's alpha property, which is just a wrapper for the layer's opacity property.

In theory, having documented that the opaque property allows them to optimize drawing, Apple could implement that optimization in the future. In practice, doing so would probably break a lot of apps, so they probably won't make such a change apply to apps linked against older SDKs. (They have the ability to make UIKit behave differently depending on which version the app was linked with.)

Alpha value for NSView subviews

I cannot think of an approach that'll work with your current view hierarchy.

What I'd do instead:

Use an additional, common super view and leave it be.

Next add two chains of subviews to that master view -

  • one you'll want to dim or reduce alpha value for,
  • a second one for subviews that'll stay 100% opaque all the time.


Related Topics



Leave a reply



Submit