Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 788
jasongregori Avatar asked Jan 27 '12 20:01

jasongregori


People also ask

What is shadow offset?

shadowOffset : the offset of the layer's shadow. The type of this property is CGSize. Width controls the shadow's horizontal offset, and the height controls its vertical offset. The default value of this property is (0.0, -3.0) .

What is shadow opacity in Swift?

shadowOpacity sets how transparent the shadow is, where 0 is invisible and 1 is as strong as possible. shadowOffset sets how far away from the view the shadow should be, to give a 3D offset effect.

What is shadow path?

shadowPath controls the shape of the shadow. This defaults to nil , which causes UIKit to render the view offscreen to figure out the shadow shape. shadowRadius controls how blurred the shadow is. This defaults to 3 points.

What is shadow radius?

A number that specifies the amount of blurring around the edges of shadows cast by the light.


1 Answers

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.

like image 173
Bartosz Ciechanowski Avatar answered Oct 17 '22 06:10

Bartosz Ciechanowski