24

In my example, I have 3 views: one red view containing two white views. I change the red container view's alpha to 0.3 and this happens (look at the image, the current result).

By seeing this, I can only assume (tell me if I'm wrong) that setting a view's alpha will also set all of its subviews' alphas. My question is : is there a way to simply tell the red view to act as a whole so that setting its alpha would give something that looks like the wanted result (in the image)?

results

This is what it looks like without any alpha :

enter image description here

Louis Boux
  • 1,228
  • 1
  • 12
  • 26
  • In your "wanted result" do you want to see background through whwite and red views or not? If not you should use custom color with alpha 1. Subviews "inherit" alpha from superview. Another way you can try is layers, but i am not sure. – Roman Temchenko Dec 20 '11 at 21:15

3 Answers3

20

To elaborate on Mark's answer: If you set UIViewGroupOpacity in the Info.plist, it will change the behavior for all views in your app, if you are interested in only fixing the rendering of this particular view, you could also use this snippet:

redContainerView.layer.shouldRasterize = YES;
// No setting rasterizationScale, will cause blurry images on retina.
redContainerView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
Torsten
  • 834
  • 9
  • 13
15

The iOS alpha property is inherited by its subviews. If we are setting alpha 0.3 for red view then both subview will have the alpha = 0.3. There is no way to stop subviews from inheriting their alpha value from their superview.

The solution might be to set the colour of the red view with an alpha of 0.3. The color property will not be inherited by its subview.

[redView setBackgroundColor:[UIColor colorWithHue:238.0f/255.0f saturation:24.0f/255.0f brightness:24.0f/255.0f alpha:0.3]];
shim
  • 9,289
  • 12
  • 69
  • 108
Nanda
  • 722
  • 5
  • 17
  • 2
    Absolutely best answer. The concept that color has alpha and view has alpha, each with different behaviors, is pretty important. – Richard Oct 09 '15 at 05:46
  • alpha value is Not inherited by subviews – lazi74 Mar 17 '16 at 11:35
  • MVP answer. This is especially easy to do in the interface builder by just clicking on a views background color and using the opacity slider. It's alpha without all the subview headaches of alpha. – Iron John Bonney Jun 24 '16 at 03:37
  • This answer provides a good workaround but it is important to remember that it isn't correct when it says "there is no way to stop ...", not even in 2014 when it was written. Also note from modern readers that on iOS7 and above the wanted behavior in the question is now enabled as default. – Christoffer Årstrand Nov 29 '17 at 12:22
12

Check out the possible UIKit keys for Info.plist, specifically UIViewGroupOpacity.

UIViewGroupOpacity (Boolean - iOS) specifies whether Core Animation sublayers inherit the opacity of their superlayer.

Info.plist UIKit Keys

Mark Adams
  • 30,776
  • 11
  • 77
  • 77
  • According to the docs, the key is no by default i.e. dont inherit. This is odd no? The default behavior seams to be to inherit. http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW6 – Robert Jun 06 '12 at 09:52
  • Correct--I don't get this either. If I don't want inheritance, do I set the key to YES or NO? I said 'NO' and that didn't fix my issues. – user798719 Apr 05 '13 at 06:13