首页 > 其他 > 详细

UIButton在不同状态下显示不同背景色

时间:2015-10-09 16:51:51      阅读:147      评论:0      收藏:0      [点我收藏+]

参考自:原文地址(内容与原文并无区别,只是自己以后方便使用整理了一下)

1.UIButton的background是不支持在针对不同的状态显示不同的颜色。

2.UIButton的backgroundImage是针对不同的状态的,所以思路就是在不同状态下的的时候,生成指定颜色的纯色图片。

 

关键代码:

/**

 *  设置不同状态下的背景色

 *

 *  @param backgroundColor 背景色

 *  @param state           状态

 */

- (void)setBackgroundColor:(UIColor *)backgroundColor State:(UIControlState)state{

        [self setBackgroundImage:[self imageWithColor:backgroundColor] forState:state];

}

 

/**

 *  生成纯色图片

 *

 *  @param color 颜色

 *

 *  @return 图片

 */

- (UIImage *)imageWithColor:(UIColor *)color{

    CGRect rect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, color.CGColor);

    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

}

 

GitHub地址:Locking-Xu_GitHub

UIButton在不同状态下显示不同背景色

原文:http://www.cnblogs.com/small-octopus/p/4864338.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!