Mimic CALayer shadow properties found in iPhone OS 3.2 for OS 3.1

Posted by niblha on Stack Overflow See other posts from Stack Overflow or by niblha
Published on 2010-05-13T13:32:44Z Indexed on 2010/05/13 13:34 UTC
Read the original article Hit count: 589

Filed under:
|
|

The CALayer shadow properties like shadowOffset, shadowRadius, shadowColor are not available in iPhone OS versions below 3.2 and I'm wondering how I could mimic that functionality for use with 3.1 and below.

I want to use this to be able to add drop shadows to UIViews in a clean way so that the shadows are drawn at layer level somehow, and not by drawing it in a view's -(void)drawRect:(CGRect)rect method which requires to shrink the actual views frame to accomodate for the shadow. (This shrinking approach have been proposed in the other UIView drop shadow related questions I found here on SO).

I was thinking a layered approach would be cleaner. For example I tried creating subclassing CALayer to which I added a separate shadow layer as a sublayer, but then that would be drawn on top of whatever was draw in the drawRect: method of the UIView that had the main layer as backing layer.

I've also tried implementing the subclass CALayer's drawInContext: something like this,

- (void)drawInContext:(CGContextRef)ctx {
// code to draw shadow for a frame the size of the layer's frame        
[super drawInContext:ctx];
}

But then the shadow is still clipped to the current clipping bounding box of the context, which seems to be the layers own frame.

I also had some idea of redirecting the drawing of the main layer to a sublayer, which would be placed above another sublayer which had the shadow drawn onto it. Then I would probably get rid of the clipping and the shadow would be farthest away. But I couldn't really wrap my head around how I would do that, and it doesn't really feel like a clean approach.

Any ideas on how to go about this? Just to make clear how my UIView drop shadow related question is different from the other ones I found here on SO; I do not want to shrink the actual drawing frame of a UIView to accomodate for a shadow. I want it to somehow be on a separate layer in the background, whithout beeing clipped.

© Stack Overflow or respective owner

Related posts about uiview

Related posts about calayer