我通過這一類方法改變UIButton
的背景顏色,使用1x1像素的圖像:突出顯示UIButton BG顏色,但保留圖層角半徑?
- (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
的設置。我需要一個帶有圓角的按鈕,但也需要一個可以突出顯示的背景顏色。
任何方法?角落半徑需要是動態的。
你爲什麼用圖像而不是UIColor設置背景顏色?如果您的圖片爲1px x 1px,則它已經是純色。或者,您可以使用UIColor方法colorWithPatternImage: – skladek 2014-09-10 15:43:55
我需要設置突出顯示的狀態背景色,UIKit不提供這樣做的手段,因此不屬於類別。但是,這樣做會殺死圓角,這就是我需要解決的問題。 – Luke 2014-09-10 15:45:14