masksToBounds vs. clipsToBounds

33,353

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).

Share:
33,353

Related videos on Youtube

sathishkumar_kingmaker
Author by

sathishkumar_kingmaker

Updated on April 09, 2021

Comments

  • sathishkumar_kingmaker
    sathishkumar_kingmaker about 3 years

    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?

    • Daij-Djan
      Daij-Djan over 7 years
      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
  • vaibhav
    vaibhav over 7 years
    can you please attach screenshot of that for more understanding..
  • vaibhav
    vaibhav over 7 years
    can you please attach screenshot of that for more understanding..
  • Bhagabata
    Bhagabata over 7 years
    Sorry Bro ...I couldn't ,don't have much reputations otherwise I could provide a nice example through screen shot
  • Bhagabata
    Bhagabata over 7 years
    If you think it is a correct ans then vote pls
  • Daij-Djan
    Daij-Djan over 7 years
    IMHO this does not explain the difference.. I guess the difference is that one is for layers, one for views ;) ultimately the same
  • MobileMon
    MobileMon over 7 years
    I wonder what will happen if clipToBounds is set to false and maskToBounds is set to true
  • Shiva
    Shiva about 7 years
    Great answer ! SAVED MY DAY
  • Graham Perks
    Graham Perks over 6 years
    The flags are identical, see stackoverflow.com/questions/1177775/….
  • Aditya Srivastava
    Aditya Srivastava about 6 years
    Your 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
    Marco Pappalardo over 4 years
    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