iOS 自定義Button的高亮和normal

//根據顏色建立一個圖片
+ (UIImage *)createImageWithColor:(UIColor *)color rect:(CGRect)rect
{
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

而後就能夠調用這個方法建立了一個UIImage的對象,接着就能夠直接調用系統的- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state 這個方法設置默認和高亮的顏色了,很方便。code

相關文章
相關標籤/搜索