UIView clipsToBounds property: Does it improve performance?
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.
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, 2020Comments
-
Thanks about 4 years
The documentation says that the
clipsToBounds
property ofUIView
will clip the drawing to the bounds, or more precisely that thesubView
can't draw outside of the bounds of thesuperView
.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 thesuperView
. 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 aUIScrollView
? -
Kriem about 15 yearsI think you're right. Simply put, the OS has to do something to make clipping happen, thus, it hurts performance.
-
Thanks about 15 yearsbut 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 about 15 yearsActually, 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 over 11 yearsIf 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 almost 11 yearsIt 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 over 10 yearson 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.