iOS button背景颜色状态设置
2016-11-22 14:11
387 查看
转自:http://blog.csdn.net/qq_20176153/article/details/52036422
关于button背景颜色(高亮状态)和(普通状态)
[self.confirmBtn setBackgroundImage:[Function imageWithColor:RGB(244, 152, 71)] forState:UIControlStateNormal];
封装类方法掉用:
// 颜色转换为背景图片
+ (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
iOS小技巧 - 为按钮设置不同状态下的背景色转自:http://www.2cto.com/kf/201412/361145.html
我们知道直接在Storyboard中设置按钮的背景色是不能根据不同状态来更改的,那问题来了,如果我们需要在不同的状态下(比如按钮没有被按下或者被按下),使得按钮呈现不同的背景色怎么办?
比如上图左边是按钮没有被按下时的背景色,右边是按钮被按下时的背景色。
第一种方案
我们知道按钮的Image属性可以在不同的状态下设置不同的图片,那最直观的办法就是提供两种背景色的图片,然后直接在Storyboard上通过设置不同状态下Image属性的值来达到目的。
但是这种方案最不好的地方就在于需要提供很多仅仅是颜色不同的图片,如果以后背景色改成其他色系怎么办?设置以后提供换肤功能,每一种皮肤都要提供一整套这些背景色图片吗?
第二种方案
我们还知道按钮的BackgroundImage也是可以根据不同状态来设置不同的背景图片的,那方案就来了,让程序根据颜色自动生成不同的纯色背景图片即可。
为了保证可复用性,定义一个UIButton的Category,实现如下:
@implementation UIButton (FillColor)
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state {
[self setBackgroundImage:[UIButton imageWithColor:backgroundColor] forState:state];
}
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
上述代码应该还是很直观的,调用的办法如下:
[self.button setBackgroundColor:GetColorFromHex(0xffff9000) forState:UIControlStateNormal];
[self.button setBackgroundColor:GetColorFromHex(0xffff6c00) forState:UIControlStateHighlighted];
其中GetColorFromHex是我自己定义的一个宏:
#define GetColorFromHex(hexColor) \
[UIColor colorWithRed:((hexColor >> 16) & 0xFF) / 255.0 \
green:((hexColor >> 8) & 0xFF) / 255.0 \
blue:((hexColor >> 0) & 0xFF) / 255.0 \
alpha:((hexColor >> 24) & 0xFF) / 255.0]
其实上述宏代码写成Category会更好,以后再做修改了。
关于button背景颜色(高亮状态)和(普通状态)
[self.confirmBtn setBackgroundImage:[Function imageWithColor:RGB(244, 152, 71)] forState:UIControlStateNormal];
封装类方法掉用:
// 颜色转换为背景图片
+ (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
iOS小技巧 - 为按钮设置不同状态下的背景色转自:http://www.2cto.com/kf/201412/361145.html
我们知道直接在Storyboard中设置按钮的背景色是不能根据不同状态来更改的,那问题来了,如果我们需要在不同的状态下(比如按钮没有被按下或者被按下),使得按钮呈现不同的背景色怎么办?
比如上图左边是按钮没有被按下时的背景色,右边是按钮被按下时的背景色。
第一种方案
我们知道按钮的Image属性可以在不同的状态下设置不同的图片,那最直观的办法就是提供两种背景色的图片,然后直接在Storyboard上通过设置不同状态下Image属性的值来达到目的。
但是这种方案最不好的地方就在于需要提供很多仅仅是颜色不同的图片,如果以后背景色改成其他色系怎么办?设置以后提供换肤功能,每一种皮肤都要提供一整套这些背景色图片吗?
第二种方案
我们还知道按钮的BackgroundImage也是可以根据不同状态来设置不同的背景图片的,那方案就来了,让程序根据颜色自动生成不同的纯色背景图片即可。
为了保证可复用性,定义一个UIButton的Category,实现如下:
@implementation UIButton (FillColor)
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state {
[self setBackgroundImage:[UIButton imageWithColor:backgroundColor] forState:state];
}
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
上述代码应该还是很直观的,调用的办法如下:
[self.button setBackgroundColor:GetColorFromHex(0xffff9000) forState:UIControlStateNormal];
[self.button setBackgroundColor:GetColorFromHex(0xffff6c00) forState:UIControlStateHighlighted];
其中GetColorFromHex是我自己定义的一个宏:
#define GetColorFromHex(hexColor) \
[UIColor colorWithRed:((hexColor >> 16) & 0xFF) / 255.0 \
green:((hexColor >> 8) & 0xFF) / 255.0 \
blue:((hexColor >> 0) & 0xFF) / 255.0 \
alpha:((hexColor >> 24) & 0xFF) / 255.0]
其实上述宏代码写成Category会更好,以后再做修改了。
相关文章推荐
- IOS button背景颜色状态设置
- iOS button背景颜色状态设置
- 设置button不同状态下的背景色,即把这个颜色变成图片设置成,背景图片
- Ios开发Swift自定义UIbutton增加一个能设置高亮状态下背景颜色的方法
- Button按钮状态背景的设置
- iOS开发UITabBar背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式
- iOS navigationbar背景颜色和button title的问题
- IOS_设置UITableView Section的背景颜色和字体颜色(自定义section布局)
- ios状态栏颜色的设置(背景、前景)
- IOS开发代码分享之设置UISearchBar的背景颜色
- 【iOS开发-31】UITabBar背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式
- iOS开发--设置button选中状态
- iOS UIWebView 设置背景颜色透明
- ios设置导航栏背景图片、返回按钮背景、标题颜色等等
- ios tableview和cell背景颜色设置
- iOS 7 UITableview 在Plain模式下 设置背景颜色无效
- ios设置导航栏背景图片、返回按钮背景、标题颜色等等
- 动态设置Button、ImageView等组件在不同状态下的背景/前景显示效果。 扩展下的话可以前景/背景的显示效果可以使用网络图片。
- iOS设置UINavigationBar的背景颜色或背景图片
- iOS状态栏文字颜色设置白色