how to add shadow to only the bottom and right side of a uiview?

tom picture tom · Apr 16, 2012 · Viewed 11.2k times · Source

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?

Answer

edc1591 picture edc1591 · Apr 16, 2012

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.