我试图使用CoreGraphics和CALayers在UIButton上绘制一些精美的图形,但我无法在子图层上显示任何内容。
以下是我设置子图层的方法:
- (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就像这样实现drawLayer: inContext:
:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
CGRect rectangleFrame = layer.bounds;
UIGraphicsPushContext(context);
UIBezierPath* rectanglePath =
[UIBezierPath bezierPathWithRect: rectangleFrame];
[[UIColor redColor] setFill];
[rectanglePath fill];
UIGraphicsPopContext();
}
代码被调用(我可以设置断点或输出到NSLog),但没有显示任何内容:我只有一个透明按钮(如主图层所示),带有和不带任何按钮文本。
我需要更改为" draw"在子层?
答案 0 :(得分:4)
[customDrawn setFrame:buttonLayer.bounds];