ios UIButton设置高亮状态下的背景色
一,通过按钮的事件来设置背景色
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
- ( 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]; } |
二,通过把颜色转换为UIImage来作为按钮不同状态下的背景图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
- ( 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; } |
三
1.首先,添加一个按钮在界面上,我们先设置好普通和高亮状态时的文字,还有圆角
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
[button setTitle:@"normal" forState:UIControlStateNormal];
[button setTitle:@"highlighted" forState:UIControlStateHighlighted];
[button setBackgroundColor:[UIColor redColor]];
button.layer.cornerRadius = 10.0f;
button.layer.masksToBounds = YES;
[self.view addSubview:button];
// 添加观察者方法
[self addObserver:button];
2.添加观察者
/**
* 添加观察者
*
* @param button 需要设置的按钮
*/
- (void)addObserver:(UIButton *)button {
[button addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew context:nil];
}
3.接下来就是实现观察者方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
UIButton *button = (UIButton *)object;
if ([keyPath isEqualToString:@"highlighted"]) {
if (button.highlighted) {
[button setBackgroundColor:[UIColor blueColor]];
return;
}
[button setBackgroundColor:[UIColor redColor]];
}
}
这样我们就能在不使用图片的前提下,实现设置高亮和普通状态下的背景颜色
看下具体的效果
ios UIButton设置高亮状态下的背景色的更多相关文章
- iOS小技巧 - 为按钮设置不同状态下的背景色
我们知道直接在Storyboard中设置按钮的背景色是不能根据不同状态来更改的,那问题来了,如果我们需要在不同的状态下(比如按钮没有被按下或者被按下),使得按钮呈现不同的背景色怎么办? 比如上图左边是 ...
- iOS - UIButton设置文字标题下划线以及下划线颜色
创建button设置可以折行显示 - (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [[UIButton alloc] in ...
- UIButton在不同状态下显示不同背景色
参考自:原文地址(内容与原文并无区别,只是自己以后方便使用整理了一下) 1.UIButton的background是不支持在针对不同的状态显示不同的颜色. 2.UIButton的backgroundI ...
- UICollectionViewCell选中高亮状态和UIButton的高亮状态和选中状态
UICollectionViewCell选中高亮状态 //设置点击高亮和非高亮效果! - (BOOL)collectionView:(UICollectionView *)collectionView ...
- UIButton在Disabled状态下标题混乱的问题
最近开发中遇到的问题汇总 有段时间没有归纳开发中遇到的一些问题了,今天就写一下之前开发中遇到的几个问题.希望这 篇文章能让读者在以后的开发中少走弯路.本文将依次介绍<UIButton在Disab ...
- iOS MJRefresh设置MJRefreshStateNoMoreData状态图片
MJRefresh地址 // 代码地址: https://github.com/CoderMJLee/MJRefresh// 代码地址: http://code4app.com/ios/%E5%B ...
- swift 取消UIButton选中高亮状态
objc可以用通过重写setHighlighted方法来达到当按钮选中时的高亮状态 -(void)setHighlighted:(BOOL)highlighted{ } swift中取消高亮状态 ov ...
- 设置button不同状态下的背景色,即把这个颜色变成图片设置成,背景图片
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state { [self setBack ...
- ios UIButton设置单选效果,以及同时设置图片和标题
一,设置单选效果 - (void)selectedBtnPress:(UIButton*)sender { //首先把原来按钮的选中效果消除 for (int i=0;i<num;i++) {/ ...
随机推荐
- 高效遍历匹配Json数据与双层for循环遍历Json数据
工作中往往遇到这种情况,保留用户操作痕迹,比如用户选择过得东西,用户进入其它页面再返回来用户选择的的数据还在. 比如:1.购物车列表中勾选某些,点击任意一项,前往详情页,再返回购物车依旧需要呈现勾选状 ...
- 自己总结的C#编码规范--前言&目录
最近在为公司编写c#编码规范,以前对这方面研究不多,只是觉得代码能够出自己的意思就可以了. 我参考了以下资料 C# Coding Conventions NET设计规范约定惯用法与模式(第2版) 编写 ...
- C#:几种数据库的大数据批量插入(转)
在之前只知道SqlServer支持数据批量插入,殊不知道Oracle.SQLite和MySql也是支持的,不过Oracle需要使用Orace.DataAccess驱动,今天就贴出几种数据库的批量插入解 ...
- Javascript中call,apply,bind的区别
一.探索call方法原理 Function.prototype.call = function(obj) { // 1.让fn中的this指向obj // eval(this.toString().r ...
- ParallaxNode视差节点实现远景近景的不同层次移动
Cocos2d-x有ParallaxNode视差节点,视察顾名思义,就是造成不同的移动速率的效果. 我想大家都玩过刀塔传奇,他的背景有远景和近景之分,而且你滑动屏幕的时候远景和近景是按照不同的速率移动 ...
- git 修改文件内容
在安装Git和创建版本库的时候,我们已经成功地添加并提交了一个readme.txt文件,现在,是时候继续工作了,于是,我们继续修改readme.txt文件,改成如下内容 [root@node1 gi ...
- 3dmax多个版本软件的安装包以及安装教程
这个文档具体出自哪里,我也是记不得了,需要的看下,链接如果是失效,那我也无能为力了. 免费分享,链接永久有效 2014版3D MAX链接:http://pan.baidu.com/s/1nuFr7Xv ...
- exce中42093和日期之间的关系
在EXECEL中数字0 代表日期 1900-1-0 ,即这个日期为起始日期,算是第0天数字1 代表日期 1900-1-1 ,即第一天数字2 代表日期 1900-1-2 ,即第二天......数字415 ...
- Spark MLlib 之 StringIndexer、IndexToString使用说明以及源码剖析
最近在用Spark MLlib进行特征处理时,对于StringIndexer和IndexToString遇到了点问题,查阅官方文档也没有解决疑惑.无奈之下翻看源码才明白其中一二...这就给大家娓娓道来 ...
- 如何Python下载大文件?
我想用python脚本下载很多文件,但是经常就有那么几个出错,写了个error handling,跳了过去,但是把出错的链接保存了一下. 转过天来,研究了一下出的什么错. 一个报错如下: PS C:\ ...