views:

166

answers:

2

I was pretty excited when I found out just how easy it is to add shadows to my UIViews on the iPhone/iPad.

Just add the framework in Xcode, add the import to the top of the file:

#import <QuartzCore/QuartzCore.h>

Then later:

self.contentView.layer.shadowRadius = 3.0;
self.contentView.layer.shadowOffset = CGSizeMake(-2.0, -3.0);
self.contentView.layer.shadowOpacity = 0.5;
self.contentView.layer.shadowColor = [UIColor blackColor].CGColor;

While this does create a beautiful shadow in my app, it also lags it to death now when the view is shown... even when launched outside of the debugger. Is there something I'm forgetting or is this method just not practical for larger views?

For reference, I posted a screenshot here.

+6  A: 

You should set the shadowPath property. It is how CoreGraphics is able to optimize shadows.

For example, if your view is an opaque rectangle:

self.contentView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.contentView.bounds].CGPath;
cobbal
Indeed, this is exactly what I was looking for. Thanks!
Moduspwnens
For those that get here from Google, though, could you change your "self.bounds" to "self.contentView.bounds" to match my example?
Moduspwnens
@Moduspwnens indeed
cobbal
A: 

For one project I just added a black translucent UIView with rounded corners below my view. It looks ok, but not great. With a textured background it looks better.

Something like this app, first screenshot for the iPad version.

Marco Mustapic