Question

I am thinking about adding drop shadow to the bottom and right side of UIView, but I found all the solutions out there are adding shadows for four sides a view. Is there a way to work around that?

Was it helpful?

Solution

I may do it by adding two UIImageViews with stretched shadow image on bottom and right side of the view. You don't have to cover all the view with those UIImageViews, you just clip as much as you need. Take a look at the color blended layers of twitter on the iPhone, I believe those beautiful shadows are created by using UIImageViews. And that saves system resources. Of course you can use CALayer to create shadow, but I think it consumes more system resources to render the shadow, so CALayer is second choice for me.

OTHER TIPS

You can use CAGradientLayer like so,

CAGradientLayer *shadow = [CAGradientLayer layer];
shadow.frame = CGRectMake(-10, 0, 10, myView.frame.size.height);
shadow.startPoint = CGPointMake(1.0, 0.5);
shadow.endPoint = CGPointMake(0, 0.5);
shadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.4f] CGColor], (id)[[UIColor clearColor] CGColor], nil];
[myView.layer addSublayer:shadow];

You'll have to change the frame to suit your needs. This example displays a shadow along the height of a view on the left. You can also change the start and end point to control the direction of the shadow.

UIBezierPath *shadowPath = [UIBezierPath 
bezierPathWithRect:self.yourViewObj.bounds];
self.yourViewObj.layer.masksToBounds = NO;
self.yourViewObj.layer.shadowColor = [UIColor blackColor].CGColor;//*** color you want for shadow
self.yourViewObj.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
self.yourViewObj.layer.shadowOpacity = 0.7f;
self.yourViewObj.layer.shadowPath = shadowPath.CGPath;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top