我通过这个分类方法,使用1像素乘1像素的图像来改变UIButton
的背景颜色:
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0);
[backgroundColor setFill];
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, 1, 1));
UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
[self setBackgroundImage:backgroundImage forState:state];
UIGraphicsEndImageContext();
}
然而,这将覆盖我设置的
.layer.cornerRadius
。我需要一个带圆角的按钮,但也需要一个可以在突出显示时更改背景颜色的按钮。有什么解决办法吗?圆角半径需要是动态的。