我需要更改自定义类按钮的bg颜色,该按钮由UIView
和UIButton
组成。
- (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;
}
我的问题是视图的圆角半径消失了,视图变成了正方形而不是保持圆形,如下所示:
答案 0 :(得分:0)
我正在调用setBackgroundColor:而不是changeBackgroundColor:,Dumb error