更改UIView backgrundColor重置圆角

时间:2017-08-09 13:09:24

标签: ios objective-c uiview calayer

我需要更改自定义类按钮的bg颜色,该按钮由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;

}

使用此方法按下UIView时,我会更改UIButton的backgroundColor。我从灰色变为绿色:

 - (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;

}

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

enter image description here

1 个答案:

答案 0 :(得分:0)

我正在调用setBackgroundColor:而不是changeBackgroundColor:,Dumb error