Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView group opacity in single view heirachy

Tags:

ios

uiview

I am writing a UI library in which i would like to be able to have the alpha of the UIViews as if the UIViewGroupOpacity info.plist ket was set. See following links:

Make UIView and subviews translucent as one layer, not individually

iOS controlling UIView alpha behaviour for subviews

But, as I am writing a lib, I dont want the projects to have to set this global key, and effect the behaviour of the main project.

Is there any other way of achieving this? I dont want to cycle through all the subviews and set alpha of each subview, as the project that includes my lib might have this key enabled...

like image 556
Nick Hingston Avatar asked May 11 '12 07:05

Nick Hingston


2 Answers

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.

like image 126
Torsten Avatar answered Nov 18 '22 18:11

Torsten


For iOS 7.0 and later:

Since iOS 7.0, this can be controlled for each CALayer individually through the allowsGroupOpacity property. By setting it to YES, you get the same behavior as if UIViewGroupOpacity was set, but just for that particular layer.

like image 27
Patrick Pijnappel Avatar answered Nov 18 '22 18:11

Patrick Pijnappel