Should the drawing of CAShapeLayers be called from drawRect()
or layoutSubviews()
?
According to Apple's documentation on UIViews, drawing operations should be placed in drawRect()
.
However this is not the case in Kevin Cathey's WWDC Session: What's New in Interface Builder (where he demonstrates the process of building a custom UIView that is accessible in Interface Builder).
During his demonstration he performs the drawing of the view in layoutSubviews
, rather than drawRect
.
His explanation was that:
If I were to implement drawRect, that's not going to get us the best performance; whereas, using sublayers and subviews is going to get us really good performance.
From what I've read so far on StackOverflow, it seems that overriding the drawRect()
method can cause reduced performance. This is partially due setNeedsDisplay
triggering manual redraws being expensive.
But looking through the Apple Documentation and real world applications. It makes sense that drawRect()
should be responsible for the drawing of the View, and layoutSubviews()
handling positioning.