title: CALayer - clear internal rendering context in favor of custom drawing code
body: <p>When subclassing an CALayer and implementing the drawInContext method, I would assume that any drawing I do within there is all that will show up, but instead if I set (for example) borderWidth/borderColor then CALayer will draw a border on it's own above all my custom drawing code.</p>
  
  <p><b>This is a CALayer subclass:</b></p>
  
  <p><code>
      @implementation MyCustomCALayer  </p>
  
  <pre><code>- (id)init  
  {  
      self = [super init];  
  
      if(self)  
      {  
          [self setNeedsDisplayOnBoundsChange:YES];  
      }  
  
      return self;  
  }  
  
  - (void)drawInContext:(CGContextRef)context  
  {  
      CGRect rect = CGContextGetClipBoundingBox(context);  
  
      CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);  
      CGContextFillRect(context, rect);  
  }  
  
  @end  
  </code></pre>
  
  <p></code></p>
  
  <p><b>Created in a UIView something like:</b></p>
  
  <p><code>
      - (void)ensureLayer<br>
      {<br>
          if(myLayer)<br>
              return;  </p>
  
  <pre><code>    myLayer = [[[MyCustomCALayer alloc] init] autorelease];  
      myLayer.borderColor = [UIColor greenColor].CGColor;  
      myLayer.borderWidth = 1;  
      myLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);  
  }  
  
  - (void)layoutSublayersOfLayer:(CALayer *)layer  
  {  
      [super layoutSublayersOfLayer:layer];  
      [self ensureLayer];  
  
      if(![[layer.sublayers objectAtIndex:0] isEqual:myLayer])  
          [layer insertSublayer:myLayer atIndex:0];  
  }  
  </code></pre>
  
  <p></code></p>
  
  <p>What happens, is the MyCustomCALayer fills a rectangle with red, this is what I would expect to see and nothing else, since i've implemented the drawInContext method, but instead I see a red rectangle with a green border on top, always on top, i've tried just about every combination I can think of to get rid of the green border being drawn and cannot figure it out.</p>
  
  <p>My reasoning is I would like to use the borderWidth and borderColor and other properties of the CALayer instead of creating my own properties, because the code that I need to draw contains a border, a fill, etc... but the rendering I need to do is not a simple shape. So the only way i've found around this is to set the borderWidth to 0 and add my own property to my subclass, like myBorderWidth, which is ridiculous.</p>
  
  <p>This is done with the latest iOS SDK, but i'd imagine it's the same for Mac.</p>
  
  <p>Hope this makes sense, any thoughts?</p>
  
  <p>Thanks!</p>