views:

691

answers:

2

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.

A: 

You could create an image simulating a shadow and just scale that, probably automatically by giving it a pointer to any view. It would then automatically adjust itself and position below that in whatever superview that view has.

This works pretty nicely. Shadow should be about 50% of screen size, so the average expansion / compression is pretty invisible. If it has a big radius you really can't tell if it's scaled or not.

dontWatchMyProfile
A: 

Get out of the sun whate shade image do your shadow show? Someone elses?

Sunshades