iOS 按钮点击改变背景色
2016-04-21 11:59
399 查看
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50,
50,
80, 40)];
btn.backgroundColor=
[UIColor colorWithRed:76/255.0 green:91/255.0blue:120/255.0 alpha:1];
[btn.layer setCornerRadius:3];
btn.layer.masksToBounds = YES;
[btn setTitle:@"button" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setBackgroundImage:[self imageWithColor:
[UIColor colorWithRed:76/255.0green:91/255.0 blue:120/255.0 alpha:1]] forStat
e:UIControlStateNormal];
[btn setBackgroundImage:[self imageWithColor:
[UIColor colorWithRed:54/255.0green:65/255.0 blue:87/255.0 alpha:1]] forState
:UIControlStateHighlighted];
[self.view addSubview:btn];
}
// 颜色转换为背景图片
- (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;
}
[super
viewDidLoad];
// Do any additional setup after loading the view.
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50,
50,
80, 40)];
btn.backgroundColor=
[UIColor colorWithRed:76/255.0 green:91/255.0blue:120/255.0 alpha:1];
[btn.layer setCornerRadius:3];
btn.layer.masksToBounds = YES;
[btn setTitle:@"button" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setBackgroundImage:[self imageWithColor:
[UIColor colorWithRed:76/255.0green:91/255.0 blue:120/255.0 alpha:1]] forStat
e:UIControlStateNormal];
[btn setBackgroundImage:[self imageWithColor:
[UIColor colorWithRed:54/255.0green:65/255.0 blue:87/255.0 alpha:1]] forState
:UIControlStateHighlighted];
[self.view addSubview:btn];
}
// 颜色转换为背景图片
- (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之Block报错:capturing 'self' strongly in this block is likely to lead to a retain cycle
- iOS 签发证书无效问题详解
- ios 解决 dyld: Library not loaded: /System/Library/Frameworks/CoreMedia.framework/CoreMedia
- iOS navgationBar 的颜色相关设置
- iOS 开发小技巧
- iOS调试
- iOS到Mac OS之NSTableView的简单使用
- iOS 子视图加入到父视图及移除父视图的过程
- [IOS/第三方工具]CocoaPods 终端上功能以及使用说明
- ios按钮长按事件
- AFNetWorking的get与post用法以及进行图片上传
- 如何控制百度地图的显示范围 ios软件开发
- ios按钮滑动切换
- 开发笔记-27个iOS开发中的小技巧
- iOS调整系统屏幕亮度
- iOS开发经验总结
- IOS9获取HTML数据报错解决
- [BS-02] iOS数组、字典、NSNumber 新写法—— @[]、@{}
- iOS设置NavigationBar颜色和BackButtonItem颜色以及navigation的应用
- IOS7 iBeacons探寻