what is relation between masksToBounds and cornerRadius?

eon picture eon · Jul 4, 2012 · Viewed 12.3k times · Source

My scroll has some views that is cornerRadius = 10.0 and masksToBounds = YES.

This causes a performance issue for scrolling my scroll view. Though I tried each view's shouldRasterize is set YES, the issue is not solved.

However, I know the solution for this issue. That is what masksToBound is set NO. (masksToBound's default value is NO) Although masksToBound is NO, my views's corners still have curve and the performance issue is solved, too.

I want to know about relation between cornerRadius and masksToBounds. I'm afraid of unexpected issues with this setting.

Thank you.

Answer

Paresh Navadiya picture Paresh Navadiya · Jul 4, 2012

After providing cornerRadius which specifies a radius used to draw the rounded corners of the receiver’s background.

Now it depends on masksToBounds which determines if the sublayers are clipped to the receiver’s bounds. So if it is set to YES, an implicit mask matching the layer bounds is applied to the layer, including the effects of the cornerRadius property. If it is set to YES and a mask property is specified, the two masks are multiplied to get the actual mask values.

For example:

If you set that on a CALayer with image contents, the image will still be drawn outside the corner radius boundary. You can solve this by setting sublayer.masksToBounds to YES; but if you do that, the shadows won’t show up because they’ll be masked out!