I have a custom view with a sublayer (CAShapeLayer) and subview (UILabel). When I create the layer in initWithCoder
and set the background color, it always shows up as black. However, if I move the code into initWithFrame
, the color shows up successfully.
Are we not supposed to create sublayers in initWithCoder
?
Here is the only way I can get my code to work:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.colorLayer = [CAShapeLayer layer];
self.colorLayer.opacity = 1.0;
[self.layer addSublayer:self.colorLayer];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.textLabel = [[UILabel alloc] initWithFrame:self.bounds];
self.textLabel.font = [UIFont primaryBoldFontWithSize:12];
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.textAlignment = NSTextAlignmentCenter;
self.textLabel.backgroundColor = [UIColor clearColor];
[self addSubview:self.textLabel];
}
return self;
}
- (void)drawRect:(CGRect)rect {
//Custom drawing of sublayer
}
UPDATE:
Turns out in my drawRect
I was setting the fill color wrong. I should have used colorLayer.fillColor = myColor.CGColor
instead of [myColor setFill]
then [path fill]