72

A UIView has a CALayer. That's pretty sure. But both seem to provide something that means the same thing.

If I'd set clipsToBounds=YES, would this also set the layer's masksToBounds=YES? Why different names? Anyone knows?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Thanks
  • 40,109
  • 71
  • 208
  • 322

3 Answers3

141

They are different names because UIView and CALayer are different and have different terminology associated with them, but they are functionally equivalent. If you disassemble clipsToBounds you will see it just calls masksToBounds (disassembly from the simulator framework, so x86):

-(BOOL)[UIView(Rendering) clipsToBounds]
    +0  3091938a  55              pushl    %ebp
    +1  3091938b  89e5            movl     %esp,%ebp
    +3  3091938d  e800000000      calll    0x30919392
    +8  30919392  59              popl     %ecx
    +9  30919393  8b4508          movl     0x08(%ebp),%eax
   +12  30919396  8b5004          movl     0x04(%eax),%edx         (CALayer)_layer
   +15  30919399  8b8186cb1301    movl     0x0113cb86(%ecx),%eax    masksToBounds
   +21  3091939f  89450c          movl     %eax,0x0c(%ebp)
   +24  309193a2  895508          movl     %edx,0x08(%ebp)
   +27  309193a5  c9              leave
   +28  309193a6  e92e211801      jmpl     0x31a9b4d9
Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
Louis Gerbarg
  • 43,356
  • 8
  • 80
  • 90
  • wow, impressive! how do you get to this details? could that be done just in xcode when building or is there a tool needed to check what's going on? – Thanks Jul 24 '09 at 17:15
  • 6
    For quick disassembly I use otool, which is part of the Developer tools. If I walking through a lot of code I tend to use otx to generate a complete assembly listing including doing all the selector lookups for me. – Louis Gerbarg Jul 24 '09 at 17:32
  • How can you tell from looking at that, that it really just calls masksToBounds? – David Zorychta Jun 19 '13 at 15:28
  • 8
    @Macmee if you move the `code sample` view above to the right hand side (see there is scroll bar on that) then you will see call to masksToBounds. – Saurabh Hooda Aug 06 '13 at 08:16
7

Some one asked what will happen if clipToBounds is set to false and maskToBounds is set to true or it can be better if someone add screenshots

I stumbled upon the same issue where I wanted to set corner radius and shadow both to the view.

but it is not possible because as soon as I set masksToBound to YES it removes the shadow but clip the content and if I set it to NO it shows the shadow but does not clip the content.

I have made the project which explains it better Understanding the ClipToBound And MasksToBound

I have come to the conclusion that CALayer is not a container but it is simply a class representing a rectangle on the screen with visual content. every drawing done on UIView goes on CALayer which causes the visual content to draw

masksToBound set to NO masksToBound set to YES

Vikram Sinha
  • 581
  • 1
  • 10
  • 25
0

I know this post is very old, but I asked this question on Apple developer forums:

What is the difference between clipsToBounds on UIView, and masksToBounds on it’s corresponding CALayer? Is there any?

When debugging I see that setting clipsToBounds = true on a UIView, automatically sets masksToBounds = true on the underlying CALayer, and vice versa.

From my understanding, UIViews are basically lightweight wrappers around CALayer, so it would make sense that clipsToBounds calls masksToBounds behind the scenes.

Is that assumption correct?

And got this answer from Apple staff:

Yes, in general UIKit covers a lot of CALayer properties like this, although not always in a "light weight" manner (but that usually means there are very good reason to use the UIKit API over the CALayer API if you have a UIView).

As such we generally recommend that where there is an equivalent UIView and CALayer method for doing something, you should use the UIView method.

So even though the two properties are the same (as user Louis Gerbarg answered all the way back in 2009), you should prefer the UIKit API if that is available.