title: iPhone iOS how to add linear gradient to a UIButton under the button's text or image?
body: <p>I'm working on creating fancy looking UIbuttons by adding linear gradient to the button. However I'm not sure at which index I need to add the gradient. The code that I have currently places the gradient over the image/text.</p>
  
  <p><strong>How can I insert a sublayer to a UIButton under the text/image sublayer?</strong> It is important for me to keep the text and the image of a button visible!</p>
  
  <pre><code>+(void)addLinearGradientToView:(UIView*)view TopColor:(UIColor*)topColor BottomColor:(UIColor*)bottomColor
  {
      for(CALayer* layer in view.layer.sublayers)
      {
          if ([layer isKindOfClass:[CAGradientLayer class]])
          {
              [layer removeFromSuperlayer];
          }
      }
      CAGradientLayer* gradientLayer = [CAGradientLayer layer];
  
      gradientLayer.startPoint = CGPointMake(0.5, 0);
      gradientLayer.endPoint = CGPointMake(0.5,1);
      gradientLayer.frame = view.bounds;
      gradientLayer.colors = [NSArray arrayWithObjects:(id)[topColor CGColor], (id)[bottomColor CGColor], nil];
      //    [view.layer addSublayer:gradientLayer];
      if(view.layer.sublayers.count&gt;0)
      {
          [view.layer insertSublayer:gradientLayer atIndex:view.layer.sublayers.count-2];
      }else {
          [view.layer addSublayer:gradientLayer];
      }
  }
  </code></pre>