iOS CALayer Shadows


Example

You can use 5 properties on each layer to configure your shadows:

  • shadowOffset - this property moves your shadow left/right or up/down
self.layer.shadowOffset = CGSizeMake(-1, -1); // 1px left and up

self.layer.shadowOffset = CGSizeMake(1, 1); // 1px down and right
  • shadowColor - this sets the color of your shadow
self.layer.shadowColor = [UIColor blackColor].CGColor;
  • shadowOpacity - this is the opacity of the shadow, from 0 to 1
self.layer.shadowOpacity = 0.2;
  • shadowRadius - this is the blur radius (equivalent of the blur property in Sketch or Photoshop)
self.layer.shadowRadius = 6;
  • shadowPath - this is an important property for performance, when unset iOS bases the shadow on the alpha channel of the view, which can be performance intensive with a complex PNG with alpha. This property lets you force a shape for your shadow and be more performant because of it.

Objective-C

self.layer.shadowPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,100)]; //this does a circular shadow

Swift 3

self.layer.shadowPath = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 100, height: 100)).cgPath