I'm trying to draw some fancy graphics on a UIButton using CoreGraphics and CALayers, but I can't get anything on a sublayer to display.
Here's how I set up my sublayer:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
CALayer *buttonLayer = self.layer;
buttonLayer.masksToBounds = YES;
buttonLayer.backgroundColor = [[UIColor clearColor] CGColor];
self.myDelegate = [[PlayerButtonLayerDelegate alloc] init];
CALayer *customDrawn = [CALayer layer];
customDrawn.delegate = self.myDelegate;
customDrawn.frame = buttonLayer.frame;
customDrawn.masksToBounds = YES;
[buttonLayer insertSublayer:customDrawn atIndex:0];
[customDrawn setNeedsDisplay];
}
return self;
}
PlayerButtonLayerDelegate just implements drawLayer: inContext:
like this:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
CGRect rectangleFrame = layer.bounds;
UIGraphicsPushContext(context);
UIBezierPath* rectanglePath =
[UIBezierPath bezierPathWithRect: rectangleFrame];
[[UIColor redColor] setFill];
[rectanglePath fill];
UIGraphicsPopContext();
}
The code gets called (I can set a breakpoint or output to NSLog), but nothing gets displayed: I just have a transparent button (as indicated by the main layer), with and without any button text.
What do I have to change to "draw" on the sublayer?
[customDrawn setFrame:buttonLayer.bounds];