How do I draw a shadow under a UIView? How do I draw a shadow under a UIView? ios ios

How do I draw a shadow under a UIView?


A by far easier approach is to set some layer attributes of the view on initialization:

self.layer.masksToBounds = NO;self.layer.shadowOffset = CGSizeMake(-15, 20);self.layer.shadowRadius = 5;self.layer.shadowOpacity = 0.5;

You need to import QuartzCore.

#import <QuartzCore/QuartzCore.h>


self.layer.masksToBounds = NO;self.layer.cornerRadius = 8; // if you like rounded cornersself.layer.shadowOffset = CGSizeMake(-15, 20);self.layer.shadowRadius = 5;self.layer.shadowOpacity = 0.5;

This will slow down the application. Adding the following line can improve performance as long as your view is visibly rectangular:

self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;


Same solution, but just to remind you: You can define the shadow directly in the storyboard.

Ex:

enter image description here