更改 UIView backgrundColor 重置圆角

Change UIView backgrundColor reset round corners

我需要更改自定义 class 按钮的背景颜色,该按钮由 UIViewUIButton 组成。

    - (instancetype)initWithFrame:(CGRect)frame color:(UIColor*)color imageName:(NSString*)imageName target:(id)target action:(SEL)action tag:(int)tag{

    self = [super init];

    if (self) {

        self.frame = frame;

        CircleStatusView * view = [[CircleStatusView alloc]initWithFrame:self.bounds color:color];
        view.tag = 22;
        [self addSubview:view];

        UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = self.bounds;
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn setImage:[[UIImage imageNamed:imageName]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        [btn setTintColor:[UIColor whiteColor]];
        btn.tag = tag;
        [self addSubview:btn];


    }
    return self;

}

我使用此方法在按下 UIButton 时更改了 UIView 的背景颜色。我从灰色变成绿色:

 - (void)changeBackgroundColor:(UIColor*)color{

        [(CircleStatusView*)[self viewWithTag:22] setBackgroundColor:color];

    }

CircleStatusView是这样制作的:

- (instancetype)initWithFrame:(CGRect)frame color:(UIColor*)color{

    self = [super init];

    if (self) {

        self.frame = frame;
        self.layer.borderColor = [UIColor whiteColor].CGColor;
        self.layer.borderWidth = 3;
        self.backgroundColor = color;
        self.clipsToBounds = YES;
        self.layer.masksToBounds = NO;
        self.layer.cornerRadius = frame.size.width/2;

        self.layer.shadowColor = [UIColor darkGrayColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(0, 2);
        self.layer.shadowOpacity = 1;
        self.layer.shadowRadius = 1.0;

    }
    return self;

}

我的问题是视图的角半径消失了,视图变成了正方形而不是圆形,如下所示:

我调用的是 setBackgroundColor: 而不是 changeBackgroundColor:,愚蠢的错误