我正在使用1px xpp图像通过此类别方法更改UIButton的背景颜色:
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(1,1),NO,0);
[backgroundColor setFill];
CGContextFillRect(UIGraphicsGetCurrentContext(),CGRectMake(0,1,1));
UIImage *backgroundImage = UIGraphicsGetimageFromCurrentimageContext();
[self setBackgroundImage:backgroundImage forState:state];
UIGraphicsEndImageContext();
}
但是,这会覆盖我的.layer.cornerRadius设置.我需要一个带圆角的按钮,但也需要一个可以突出显示的背景颜色.
有什么方法吗?角半径需要是动态的.
解决方法
所以,我所要做的就是确保打开了button.layer.masksToBounds.问题解决了,不需要子类化.