Search code examples
ioscalayershadowretina-display

Why is my CALayer shadow blurry on retina displays when using shadowPath with shadowRadius set to 0?


I have a button which I'm adding a shadow to like this:

b.backgroundColor = [UIColor colorWithRed:0.820 green:0.878 blue:0.941 alpha:1.000];
b.layer.cornerRadius = 3;
b.layer.shadowOffset = CGSizeMake(2, 2);
b.layer.shadowOpacity = 0.2;
b.layer.shadowRadius = 0;

That works great:

retina shadow without shadow path

If I use shadow path like so:

b.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:b.bounds cornerRadius:b.layer.cornerRadius].CGPath;

The shadow is blurry on retina devices:

enter image description here

It's fine on non retina devices and seems to work correctly if shadowRadius isn't zero. I've tried setting contentsScale and rasterizationScale to 2 but it doesn't affect the shadow.

Any other ideas?


Solution

  • As of iOS 9.0 the contentsScale property of CALayer will also affect the rasterization scale of shadowPath. In other words, setting layer's contentsScale to screen scale will fix your issue.