masksToBounds vs. clipsToBounds
Solution 1
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
Solution 2
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).
Related videos on Youtube
sathishkumar_kingmaker
Updated on April 09, 2021Comments
-
sathishkumar_kingmaker about 3 years
From my point of view both UIView
clipsToBounds
and CALayermasksToBounds
do the same job.I couldn't find any difference between them.
Can somebody kindly explain how they are different?
-
Daij-Djan over 7 yearsIMHO 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
-
-
vaibhav over 7 yearscan you please attach screenshot of that for more understanding..
-
vaibhav over 7 yearscan you please attach screenshot of that for more understanding..
-
Bhagabata over 7 yearsSorry Bro ...I couldn't ,don't have much reputations otherwise I could provide a nice example through screen shot
-
Bhagabata over 7 yearsIf you think it is a correct ans then vote pls
-
Daij-Djan over 7 yearsIMHO this does not explain the difference.. I guess the difference is that one is for layers, one for views ;) ultimately the same
-
MobileMon over 7 yearsI wonder what will happen if clipToBounds is set to false and maskToBounds is set to true
-
Shiva about 7 yearsGreat answer ! SAVED MY DAY
-
Graham Perks over 6 yearsThe flags are identical, see stackoverflow.com/questions/1177775/….
-
Aditya Srivastava about 6 yearsYour answer looks intersting but it's tough to understand without screenshot or example. You can provide the example with screenshot, I will surely upvote.
-
Marco Pappalardo over 4 yearsthey 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