title: Modern technique of adding gradient to UIView
body: <p>I know of several ways of adding background gradients to UIView. I was wondering what is the most efficient and scalable way of doing so, and why? Here are the techniques I've used:</p>
  
  <ul>
  <li><p>Create subview of UIView and overwrite the drawRect, where I draw the gradient in current context.</p>
  
  <p>a. When using the above gradient create it with the bounds of view I want to decorate and insert this background gradient as first subview, i.e. <code>– insertSubview:atIndex:</code></p>
  
  <p>b. After I get the background gradient view from above, I render it in image context and use it as background image, i.e.</p>
  
  <pre>
  UIGraphicsBeginImageContext(gradView.bounds.size);
  [gradView.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *gradientImg = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  UIColor *background = [UIColor colorWithPatternImage:gradientImg];
  self.view.backgroundColor = background;
  </pre></li>
  <li><p>Create stretchable PNG and use that to decorate view background. Technique is very similar to the way you decorate UIButton.</p>
  
  <pre>
  -(UIColor *)backgroundColor:(CGRect)frame
  {
      UIImage *bg = [UIImage imageNamed:@"background_23x36"];
      bg = [bg resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 11.0, 0.0, 11.0)];
      UIGraphicsBeginImageContextWithOptions(frame.size, YES, [[UIScreen mainScreen] scale]);
      [bg drawInRect:frame];
      UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return [UIColor colorWithPatternImage:image];
  }
  </pre></li>
  <li><p>Create subview of UIView but instead of overwriting the drawRect, use CAGradientLayer. Similar to <a href="http://nscookbook.com/2013/04/recipe-20-using-cagradient-layer-in-a-custom-view/" rel="nofollow">http://nscookbook.com/2013/04/recipe-20-using-cagradient-layer-in-a-custom-view/</a></p></li>
  </ul>
  
  <p>So what is most efficient and scalable way to add gradient as a background? </p>