更改突出显示的UIButton BG颜色,但保留层角半径?

Luk*_*uke 6 objective-c uibutton cglayer ios

我正在UIButton使用1px xpp图像更改通过此类别方法的背景颜色:

- (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();
}
Run Code Online (Sandbox Code Playgroud)

但是,这会覆盖我的设置.layer.cornerRadius.我需要一个带圆角的按钮,但也需要一个可以突出显示的背景颜色.

有什么方法吗?角半径需要是动态的.

Luk*_*uke 12

所以,我所要做的就是确保button.layer.masksToBounds打开它.问题解决了,不需要子类化.