Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 子层仅在initWithFrame中创建时绘制,而不是在initWithCoder中创建_Ios_Objective C_Uiview_Cashapelayer_Initwithframe - Fatal编程技术网

Ios 子层仅在initWithFrame中创建时绘制,而不是在initWithCoder中创建

Ios 子层仅在initWithFrame中创建时绘制,而不是在initWithCoder中创建,ios,objective-c,uiview,cashapelayer,initwithframe,Ios,Objective C,Uiview,Cashapelayer,Initwithframe,我有一个带有子层(CAShapeLayer)和子视图(UILabel)的自定义视图。当我在initWithCoder中创建图层并设置背景色时,它总是显示为黑色。但是,如果我将代码移动到initWithFrame,颜色将成功显示 我们不应该在initWithCoder中创建子层吗 以下是使代码正常工作的唯一方法: - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) {

我有一个带有子层(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]

initWithFrame:
initWithCoder:
之间的区别是,当从故事板/nib创建视图时,调用
initWithCoder:

如果以编程方式添加,例如:

UIView *v = [[UIView alloc] initWithFrame:...];
[self.view addSubview:v];
-(void)baseInit {
    self.colorLayer = [CAShapeLayer layer];
    self.colorLayer.opacity = 1.0;
    //... other initialisation
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self baseInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {

        [self baseInit];
    }

    return self;
}
调用了initWithFrame:

好主意是创建基本init方法并在两个init中调用它。这样,当以编程方式或在情节提要中添加视图时,初始化将设置这两种场景中的所有属性

例如:

UIView *v = [[UIView alloc] initWithFrame:...];
[self.view addSubview:v];
-(void)baseInit {
    self.colorLayer = [CAShapeLayer layer];
    self.colorLayer.opacity = 1.0;
    //... other initialisation
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self baseInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {

        [self baseInit];
    }

    return self;
}

但是当我在同一个地方创建子层和uilabel时(例如initWithCoder),子层的背景总是黑色的。我不明白为什么会这样。此外,显然从未调用initWithFrame,但子层仍然初始化。如果你说的是真的,那就没有意义了。如果你不相信,只需将log添加到这两个init中,你就会看到只有一个会被调用。尝试将图层添加到视图中,然后将标签图层添加到子图层:[self.colorLayer addSublayer:self.textLabel.layer];[self.layer addSublayer:self.colorLayer];