50

From my point of view both UIView clipsToBounds and CALayer masksToBounds do the same job.

I couldn't find any difference between them.

Can somebody kindly explain how they are different?

pkamb
  • 33,281
  • 23
  • 160
  • 191
  • 1
    IMHO answers here don't explain explain the difference.. I guess the difference is that one is for layers, one for views ;) ultimately the same ... the linked question's answer is good – Daij-Djan Dec 25 '16 at 08:49

2 Answers2

48

masksToBounds

Any sublayers of the layer that extend outside its boundaries will be clipped to those boundaries. Think of the layer, in that case, as a window onto its sublayers; anything outside the edges of the window will not be visible. When masksToBounds is NO, no clipping occurs.

When the value of this property is true, Core Animation creates an implicit clipping mask that matches the bounds of the layer and includes any corner radius effects. If a value for the mask property is also specified, the two masks are multiplied to get the final mask value.

you can get the more information in API Reference.

clipToBounds

The use case for clipsToBounds is more for subviews which are partially outside the main view. For example, I have a (circular) subview on the edge of its parent (rectangular) UIView. If you set clipsToBounds to YES, only half the circle/subview will be shown. If set to NO, the whole circle will show up. Just encountered this so wanted to share

for more information sample link

Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • 5
    can you please attach screenshot of that for more understanding.. – vaibhav Sep 13 '16 at 09:38
  • 36
    IMHO this does not explain the difference.. I guess the difference is that one is for layers, one for views ;) ultimately the same – Daij-Djan Dec 25 '16 at 08:47
  • Great answer ! SAVED MY DAY – Shiva Apr 07 '17 at 10:44
  • 1
    The flags are identical, see https://stackoverflow.com/questions/1177775/how-is-the-relation-between-uiviews-clipstobounds-and-calayers-maskstobounds. – Graham Perks Jan 18 '18 at 15:52
  • 6
    they are not identical: with maskToBounds is possible to display a shadow around a cornered view for example. while clipToBounds will also clip the shadow, as the layer of the shadow will fall off the mask of the view – Marco Pappalardo Aug 29 '19 at 13:59
28

clipsToBounds : with clipsToBounds set to YES, I'll only see the part of the subview that fits within the bounds of the superview. Otherwise, if clipsToBounds is set to NO, I'll see the entire subview, even the parts outside the superview 

masksToBounds: If the masksToBounds property is set to YES, any sublayers of the layer that extend outside its boundaries will be clipped to those boundaries. Think of the layer, in that case, as a window onto its sublayers; anything outside the edges of the window will not be visible. When masksToBounds is NO, no clipping occurs, and any sublayers that extend outside the layer's boundaries will be visible in their entirety (as long as they don't go outside the edges of any superlayer that does have masking enabled).

heyfrank
  • 5,291
  • 3
  • 32
  • 46
Bhagabata
  • 463
  • 4
  • 12