16

The documentation says that the clipsToBounds property of UIView will clip the drawing to the bounds, or more precisely that the subView can't draw outside of the bounds of the superView.

Sounds nice, but what does that mean in practice?

If I set that to YES, then my subView will automatically only draw those parts which are not outside the bounds of the superView. so it increases the overall performance or do I still have to make sure that I don't create any views that are not visible, i.e. inside a UIScrollView ?

Sujay
  • 2,510
  • 2
  • 27
  • 47
Thanks
  • 40,109
  • 71
  • 208
  • 322

3 Answers3

25

I think it's the opposite: turning on clipping hurts performance because it has to set up a clipping mask. I vaguely remember reading this somewhere, but I can't find it now.

Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
  • 2
    I think you're right. Simply put, the OS has to do something to make clipping happen, thus, it hurts performance. – Kriem May 04 '09 at 17:03
  • 2
    but the clipping itself is not such a thing that the system tells any method that wants to draw outside the bounds: "hey man, don't do. save your time, get a cup of tea. don't have to draw here!"? – Thanks May 04 '09 at 19:41
  • Actually, it is. Behind the scenes, the OS is doing OpenGl-like stuff where it has to decide what to draw and not. The deciding requires CPU-cycles. – Kriem May 07 '09 at 19:59
  • 2
    If iOS is doing it in OpenGL, then it's taking GPU cycles, not CPU cycles, which is a very big difference. If it is in fact GPU powered, the performance hit of the clipping itself is probably negligible. – Nick Forge Sep 12 '12 at 00:33
  • 1
    It definitely slows things down, at least in the UITableViewCell. I have 5 programmatically created UIButton objects added to the content view of each cell view in my table view. To get rounded corners for my buttons I added the standard 2 lines (self.layer.cornerRadius = numPixes;self.clipsToBounds = YES;) but scrolling suddently slowed down enormously. I commented out the clipsToBounds assignment and things returned to normal. – Alyoshak Jul 08 '13 at 22:12
  • on the contrary clipping will accelerate drawing if part of it happens outside of the bounds, it is WAY faster to compare 2 coordinates (the bounds of your view) to the clipping bounds (no it's not a mask, just a rectangle) than to do almost any kind of drawing. in the example above a rectangle with rounded corners is used, that is a special case, and will slow down drawing, clipping against a rounded rectangle is very expensive, but that isn't typical. – Pizzaiola Gorgonzola Sep 05 '13 at 21:59
1

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.

Kwok Pan Fung
  • 416
  • 5
  • 6
0

The (possible) performance hit is only deterministic if you know the view hierarchy. As mentioned above, usually the renderer will use GPU cycles to draw the view UNLESS some view within the hierarchy uses drawRect:. This does not affect OpenGL ES application because drawRect:is omitted in this type of apps.

From my understanding, determining and displaying the clipped area may take less cycles than actually calculating/drawing/blending the whole view. As of OpenGL ES 2.0 clipping can be calculated in GPU.

der_michael
  • 3,151
  • 1
  • 24
  • 43