button点击设置背景色
来源:互联网 发布:怎么查网络ip地址 编辑:程序博客网 时间:2024/05/20 07:59
1,通过按钮的事件来设置背景色
- (void)viewDidLoad { [super viewDidLoad]; UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 50)]; [button1 setTitle:@"button1" forState:UIControlStateNormal]; button1.backgroundColor = [UIColor orangeColor]; [button1 addTarget:self action:@selector(button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown]; [button1 addTarget:self action:@selector(button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button1];}// button1普通状态下的背景色- (void)button1BackGroundNormal:(UIButton *)sender{ sender.backgroundColor = [UIColor orangeColor];}// button1高亮状态下的背景色- (void)button1BackGroundHighlighted:(UIButton *)sender{ sender.backgroundColor = [UIColor greenColor];}
2,通过把颜色转换为UIImage来作为按钮不同状态下的背景图片
- (void)viewDidLoad { [super viewDidLoad]; UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(170, 200, 100, 50)]; [button2 setTitle:@"button2" forState:UIControlStateNormal]; [button2 setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateNormal]; [button2 setBackgroundImage:[self imageWithColor:[UIColor grayColor]] forState:UIControlStateHighlighted]; [self.view addSubview:button2];}// 颜色转换为背景图片- (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;}
0 0
- button点击设置背景色
- 设置Button背景色与点击涟漪颜色(5.0+)
- Button形状和背景色的设置
- android button 代码设置背景色
- 背景色渐变与Button点击效果与Button圆角
- listview设置item点击背景色
- ListView 设置点击项目背景色
- eui.Button改变文本、背景色及是否可点击
- 设置button按钮背景色,及字体的颜色
- android设置背景色为透明,Button控件透明
- Android button设置背景色与边框颜色
- UITableView 点击背景色
- 点击选择背景色
- Android改变button背景色与获取button背景色
- 改变Push Button背景色
- 改变Button的背景色
- 改变Push Button背景色
- android button 透明背景色
- 图片缓存框架Picasso的学习和使用
- 缓存服务器memcached性能测试
- 插入排序java实现
- Maven学习总结(25)——Eclipse Maven Update 时JDK版本变更问题
- 网络安全协议SSL的知识梳理
- button点击设置背景色
- 换个姿势聊聊自定义 View
- spark 安装 for mac
- 系统新消息
- AndroidStudio 修改 Master Password
- 4、Oracle PL/SQL编译错误查看与处理
- spring 配置文件浅析
- 重写view画圆中间文字
- 【leetcode】String to Integer (atoi)