title: Custom UIView drawing with layoutSubviews vs drawRect
body: <p><strong>Should the drawing of CAShapeLayers be called from <code>drawRect()</code> or <code>layoutSubviews()</code>?</strong></p>
  
  <p>According to Apple's documentation on UIViews, drawing operations should be placed in <code>drawRect()</code>.</p>
  
  <p>However this is not the case in Kevin Cathey's WWDC Session: <a href="https://developer.apple.com/videos/wwdc/2014/#411">What's New in Interface Builder</a> (where he demonstrates the process of building a custom UIView that is accessible in Interface Builder).</p>
  
  <p>During his demonstration he performs the drawing of the view in <code>layoutSubviews</code>, rather than <code>drawRect</code>.</p>
  
  <p>His explanation was that:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>From what I've read so far on StackOverflow, it seems that overriding the <code>drawRect()</code> method can cause reduced performance. This is partially due <code>setNeedsDisplay</code> triggering manual redraws being expensive.</p>
  
  <p>But looking through the Apple Documentation and real world applications. It makes sense that <code>drawRect()</code> should be responsible for the drawing of the View, and <code>layoutSubviews()</code> handling positioning.</p>