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++) {/ ...
随机推荐
- POJ 1182 食物链 【带权并查集】
<题目链接> 题目大意: 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我 ...
- 查找mac下腾讯视频下载地址
mac 腾讯视频下载的视频是不可见的,也许是因为版权原因吧.使用以下方法可以在文件中找到缓存的视频(不过都是被断开的很多短视频). 在terminal输入: cd Library/Containers ...
- python新手总结(二)
random模块 随机小数 random uniform 随机整数 randint randrange 随机抽取 choice sample 打乱顺序 shuffle random.random() ...
- 深入剖析ConcurrentHashMap
原文是09年时写的,在公司的邮件列表发过,同事一粟 和清英 创建的并发编程网 对这方面概念和实战有更好的文章,贴出来仅供参考.pdf格式在:http://www.slideshare.net/hong ...
- @Resource注解的官方解释
一.@Resource注解的官方解释@Resource annotation, which is semantically defined to identify a specific target ...
- 关于Git的总结
首先我们先看一张图: 首先我们必须要先理解这几个概念:暂存区,本地仓库,远程仓库 暂存区:这个是我们每一次进行代码修改的地方,例如我们ieda的所编译的代码就是缓存区 本地仓库:是我们每一次pull, ...
- [BZOJ3674]可持久化并查集加强版&[BZOJ3673]可持久化并查集 by zky
思路: 用主席树维护并查集森林,每次连接时新增结点. 似乎并不需要启发式合并,我随随便便写了一个就跑到了3674第一页?3673是这题的弱化版,本来写个暴力就能过,现在借用加强版的代码(去掉异或),直 ...
- 老菜鸟学习:Javascript 将html转成pdf
起因:处理某个项目,需要把页面上的数据(订单.运单)等导出pdf. 第一个想法:从 Java 层去想.但是经过各种资料查询和实践,第一个想法宣告放弃: 幸好客户的要求是:导出的 pdf 尺寸要和打印的 ...
- no console to display at this time
no console to display at this time我把控制台关掉,重新run as 还是同样问题,于是运行其他项目,但是其他项目能正常运行,说明项目写的有问题,而不是控制台的问题
- sql ,内连接,外连接,自然连接等各种连接
1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等联接和自然联接. 内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 students和c ...