title: Using CAGradientLayer as backround while also drawing in context with drawRect:
body: <p>I'm writing an app that plots some data into a simple graph and sometimes want to draw out the scale in the background. To do this I have a UIView subclass which acts as the graph background and simply use the drawRect: method to draw the scale (data elements will get added as subviews to this view, since I want the user to be able to interact with them).</p>
  
  <p>However, I also want a gradient background color and have used CAGradientLayer for this purpose (as suggested in <a href="https://stackoverflow.com/questions/422066/gradients-on-uiview-and-uilabels-on-iphone">this thread</a>). But when I add this as a sublayer, the gradient background appears, but nothing I do in drawRect: shows!</p>
  
  <p>I'm sure I'm missing something simple or have misunderstood how to use CAGradientLayer or something, so any help or suggestions is appreciated!</p>
  
  <p>This is the relevant code in my UIView subclass:</p>
  
  <pre><code>- (id)initWithFrame:(CGRect)frame {
  
      if (self = [super initWithFrame:frame]) {
  
           // Create a gradient background
          CAGradientLayer *gradientBackground = [CAGradientLayer layer];
          gradientBackground.frame = self.bounds;
          gradientBackground.colors = [NSArray arrayWithObjects:(id)[[UIColor grayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
          [self.layer insertSublayer:gradientBackground atIndex:0];
      }
  
      return self;
  }
  
  - (void)drawRect:(CGRect)rect {
  
      // Get the graphics context
      CGContextRef context = UIGraphicsGetCurrentContext();
  
      // Clear the context
      CGContextClearRect(context, rect);
  
      // Call actual draw method 
      [self drawInContext:context];
  }
  
  -(void)drawInContext:(CGContextRef)context {
  
  CGFloat step;
  
  // Draw Y scale
  if (displayYScale) {
  
      CGContextSetRGBStrokeColor(context, 0, 0, 0, kScaleLineAlpha);
  
      if (yAxisScale.mode == FCGraphScaleModeData) {
  
          step = (self.frame.size.height-(yAxisScale.padding*2))/yAxisScale.dataRange.maximum;
          for (int i = 0; i &lt;= yAxisScale.dataRange.maximum; i++) {
  
              CGContextMoveToPoint(context, 0.0f, (step*i)+yAxisScale.padding);
              CGContextAddLineToPoint(context, self.frame.size.width, (step*i)+yAxisScale.padding);
          }
  
      } else if (yAxisScale.mode == FCGraphScaleModeDates) {
  
          int units = (int)[yAxisScale units];
          step = (self.frame.size.height-(yAxisScale.padding*2))/units;
          for (int i = 0; i &lt;= units; i++) {
  
              CGContextMoveToPoint(context, 0.0f, (step*i)+yAxisScale.padding);
              CGContextAddLineToPoint(context, self.frame.size.width, (step*i)+yAxisScale.padding);
          }
      }
  
      CGContextStrokePath(context);
  }
  
  // Draw X scale
  if (displayXScale) {
  
      CGContextSetRGBStrokeColor(context, 0, 0, 255, kScaleLineAlpha);
  
      if (xAxisScale.mode == FCGraphScaleModeData) {
  
          step = (self.frame.size.width-(xAxisScale.padding*2))/xAxisScale.dataRange.maximum;
          for (int i = 0; i &lt;= xAxisScale.dataRange.maximum; i++) {
  
              CGContextMoveToPoint(context, (step*i)+xAxisScale.padding, 0.0f);
              CGContextAddLineToPoint(context, (step*i)+xAxisScale.padding, self.frame.size.height);
          }
  
      } else if (xAxisScale.mode == FCGraphScaleModeDates) {
  
          int units = (int)[xAxisScale units];
          step = (self.frame.size.width-(xAxisScale.padding*2))/units;
          for (int i = 0; i &lt;= units; i++) {
  
              CGContextMoveToPoint(context, (step*i)+xAxisScale.padding, 0.0f);
              CGContextAddLineToPoint(context, (step*i)+xAxisScale.padding, self.frame.size.height);
          }
      }
  
      CGContextStrokePath(context);
  }
  }
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>/Anders</p>