UIView clipsToBounds property: Does it improve performance?

24,623

Solution 1

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.

Solution 2

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.

Share:
24,623
Thanks
Author by

Thanks

I like to play guitar. Sometimes I need to develop software. But I hate it ;) I mean... it sucks. It really does. Well, not always. Oh, and I think I'm the guy with the most questions here.

Updated on March 05, 2020

Comments

  • Thanks
    Thanks about 4 years

    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 ?

  • Kriem
    Kriem about 15 years
    I think you're right. Simply put, the OS has to do something to make clipping happen, thus, it hurts performance.
  • Thanks
    Thanks about 15 years
    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!"?
  • Kriem
    Kriem about 15 years
    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.
  • Nick Forge
    Nick Forge over 11 years
    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.
  • Alyoshak
    Alyoshak almost 11 years
    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.
  • Pizzaiola Gorgonzola
    Pizzaiola Gorgonzola over 10 years
    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.