iOS Controlling Uiview Alpha Behaviour for Subviews

iOS controlling UIView alpha behaviour for subviews

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

How to make a UIView's subviews' alpha change according to it's parent's alpha?

Here you go:

self.view.layer.allowsGroupOpacity = YES; 

This will work for all subviews, subviews of subviews and all that.

From the docs:

 * The default value of the property is read from the boolean
* UIViewGroupOpacity property in the main bundle's Info.plist. If no
* value is found in the Info.plist the default value is YES for
* applications linked against the iOS 7 SDK or later and NO for
* applications linked against an earlier SDK. */

Your project might not be linked against the iOS 7 SDK. If you want this change to work for all views, I'd do this:

  • Go to your .plist
  • Add a row titled "Renders with group opacity"
  • Set it to YES

From Apple's docs:

UIViewGroupOpacity
“Renders with group opacity”
Specifies whether Core Animation layers inherit the opacity of their superlayer. See “UIViewGroupOpacity” for details.
iOS 3.0 and later

Does UserInteractionEnabled work like Alpha for a UIView Hierarchy?

Yes, but not in the same way. Basically, if you set a UIView.userInteractionEnable to NO, It will no longer process touch events, which means it will not be passing events to sub-views. You can, however, set userInteractionEnable to NO for sub-views with out impacting parent views or peer views.

How to correctly set alpha of UIView? [iOS]

 [view setBackgroundColor:[[UIColor clearColor] colorWithAlphaComponent:0.5]];

//try this.. dont try to set alpha of UIView and also your subviews will not affect

Different alpha for subviews in the same container

If I understand your question correctly, you should make your container view completely transparent (backgroundColor = [UIColor clearColor]) and add the opaque navigation bar and a semi-opaque content view to the container view.

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.

UIView group opacity in single view heirachy

Yes there is, you can set shouldRasterize of the view's layer.

containerView.layer.shouldRasterize = YES;
// Not setting rasterizationScale, will cause blurry images on retina displays:
containerView.layer.rasterizationScale = [[UIScreen mainScreen] scale];

This will have the same effect as UIViewGroupOpacity but only for containerView and it's subviews.



Related Topics



Leave a reply



Submit