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++) {/ ...
随机推荐
- windows server 2003 安全加固(二)
windows server 2003 安全加固 关闭默认端口 我们知道远程桌面服务端口默认开启在3389端口,如果我们一定要用到,最好能换到另外的端口上,放到靠后的端口号上去,比如10001. 更改 ...
- django-访问控制
django自带的用户认证系统提供了访问控制的的功能. 1.只允许登录的用户登录 django的用户可分为两类,一是可认证的用户,也就是在django.contrib.auth.models. ...
- C# 服务端获取客户端 系统/浏览器/IP
/// <summary> /// 获取客户端操作系统版本 /// </summary> /// <returns></returns> public ...
- LOJ.115.[模板]无源汇有上下界可行流(Dinic)
题目链接 参考:http://blog.csdn.net/clove_unique/article/details/54884437 http://blog.csdn.net/wu_tongtong/ ...
- 潭州课堂25班:Ph201805201 爬虫基础 第十三课 cookie (课堂笔记)
# -*- coding: utf-8 -*- # 斌彬电脑 # @Time : 2018/9/15 0015 4:52 #cookie 是服务器发给浏览器的特殊信息 # 可以理解为一个临时通行证 # ...
- bootStrap中的ul导航3-垂直导航
<div class="container"> <br/> <ul class="nav nav-pills nav-stacked&quo ...
- 2017-2018-2 1723 『Java程序设计』课程 结对编程练习_四则运算第三周总结
相关测试过程截图 测试了由中缀表达式转后缀表达式的Junit测试,分别进行了整数的和分数的,测试成功截图 由于生成问题和计算问题中,问题都是随机产生的,暂时不会进行Junit测试,故没有进行,但应是正 ...
- C# 的枚Enum
简短的解释: enum 关键字用来声明枚举,一种包含一组被称为枚举数列表的 enum myType{ a, b, c,} int num = 1;Console.Write((myType)num); ...
- Linux学习笔记13—Vi编辑器的学习
文本编辑工具vim.vi1. vim与vi的最大区别是vim编辑的时候是带颜色显示的.Vi不带颜色显示.2. yum install -y vim-enhanced 如果没有安装VIM 使用上面的命令 ...
- IE6的3像素bug
IE6的3像素bug3像素bug是IE6的一个著名的bug,当浮动元素与非浮动元素相邻时,这个3像素的Bug就会出现.看下面这个左列固定,右列液态的例子,css代码如下: body { margin: ...