サブレイヤー (CAShapeLayer) とサブビュー (UILabel) を持つカスタム ビューがあります。でレイヤーを作成しinitWithCoder
て背景色を設定すると、常に黒く表示されます。ただし、コードを に移動するinitWithFrame
と、色が正常に表示されます。
でサブレイヤーを作成することは想定されていませんinitWithCoder
か?
コードを機能させる唯一の方法は次のとおりです。
- (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
}
更新:
drawRect
塗りつぶしの色を間違って設定していたことが判明しました。colorLayer.fillColor = myColor.CGColor
その代わりに使うべきだっ[myColor setFill]
た[path fill]