iOS每日一记之---------改变Btn点击时的背景颜色
2015-08-13 17:25
726 查看
今天UI设计师妹纸让我改变按钮的背景颜色 但是并没有给我图片。。。。于是我就上网找了一下 大体上有俩种方法实现 一个是调用按钮的 touchDown方法;另一个是把颜色装换成图片
废话不多说 上代码
UIButton *securitycodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
securitycodeButton.frame = CGRectMake(DCS_SCREEN_W-100, 232, 90, 30);
self.securitycodeButton = securitycodeButton;
securitycodeButton.backgroundColor = DCS_COLOR_hui_background_color;//这个是Btn没有选择状态的背景颜色
[securitycodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
[securitycodeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
securitycodeButton.titleLabel.font = [UIFont systemFontOfSize:12];
[securitycodeButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];//按钮点击时触发的方法
这里添加一个按钮的 touchDown方法就实现了
[securitycodeButton addTarget:self action:@selector(didBtnColorChange:) forControlEvents:UIControlEventTouchDown];
//点击按钮方法的实现
- (void)btnClick:(UIButton *)securitycodeButton
{
securitycodeButton.backgroundColor = DCS_COLOR_hui_background_color; //依旧写上没有选择时候的颜色
}
//实现按钮的touchDown方法
-(void)didBtnColorChange:(UIButton *)securitycodeButton
{
securitycodeButton.backgroundColor =DCSColor(211, 211, 211); //这里写上你想要改变的颜色就行了
}
方法二是把颜色装换成image 然后调用setbackgroundimage这个方法
//颜色转换成image
- (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;
}
UIImage *image = [self imageWithColor: //这里填入你要改变的颜色即可];
OK 就这样
废话不多说 上代码
UIButton *securitycodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
securitycodeButton.frame = CGRectMake(DCS_SCREEN_W-100, 232, 90, 30);
self.securitycodeButton = securitycodeButton;
securitycodeButton.backgroundColor = DCS_COLOR_hui_background_color;//这个是Btn没有选择状态的背景颜色
[securitycodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
[securitycodeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
securitycodeButton.titleLabel.font = [UIFont systemFontOfSize:12];
[securitycodeButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];//按钮点击时触发的方法
这里添加一个按钮的 touchDown方法就实现了
[securitycodeButton addTarget:self action:@selector(didBtnColorChange:) forControlEvents:UIControlEventTouchDown];
//点击按钮方法的实现
- (void)btnClick:(UIButton *)securitycodeButton
{
securitycodeButton.backgroundColor = DCS_COLOR_hui_background_color; //依旧写上没有选择时候的颜色
}
//实现按钮的touchDown方法
-(void)didBtnColorChange:(UIButton *)securitycodeButton
{
securitycodeButton.backgroundColor =DCSColor(211, 211, 211); //这里写上你想要改变的颜色就行了
}
方法二是把颜色装换成image 然后调用setbackgroundimage这个方法
//颜色转换成image
- (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;
}
UIImage *image = [self imageWithColor: //这里填入你要改变的颜色即可];
OK 就这样
相关文章推荐
- C#获取电脑硬件信息(CPU ID、主板ID、硬盘ID、BIOS编
- ios afnetworking 错误解决
- 我的iOS高效编程秘诀—坚持编程习惯
- iOS小白进阶之旅(一)
- iOS 开发中农历与公历的转换,星座计算。
- nagios服务的搭建
- iOS开发日记1-tableview编辑
- Ios日志打印
- iOS开发~终极教程索引
- iOS 获取本地设备相关信息,如:IP地址
- iOS数据持久化---对象归档
- iOS 系统定位的说明与使用
- Post,同步实现登陆验证(2)
- iOS上关于状态栏的设置
- iOS 静态库和动态库.a和framework
- iOS开发 关于终端-4CocoaPods安装和使用(图文并茂版)
- PlistBuddy a good friend to handle iOS plist file
- iOS tabbar 上面更换任意图
- ios 7 statusbar 状态栏 跟随navigationbar的颜色而改变
- iOS开发 汉字转拼音