一,通过按钮的事件来设置背景色

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设置高亮状态下的背景色的更多相关文章

  1. iOS小技巧 - 为按钮设置不同状态下的背景色

    我们知道直接在Storyboard中设置按钮的背景色是不能根据不同状态来更改的,那问题来了,如果我们需要在不同的状态下(比如按钮没有被按下或者被按下),使得按钮呈现不同的背景色怎么办? 比如上图左边是 ...

  2. iOS - UIButton设置文字标题下划线以及下划线颜色

    创建button设置可以折行显示 - (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [[UIButton alloc] in ...

  3. UIButton在不同状态下显示不同背景色

    参考自:原文地址(内容与原文并无区别,只是自己以后方便使用整理了一下) 1.UIButton的background是不支持在针对不同的状态显示不同的颜色. 2.UIButton的backgroundI ...

  4. UICollectionViewCell选中高亮状态和UIButton的高亮状态和选中状态

    UICollectionViewCell选中高亮状态 //设置点击高亮和非高亮效果! - (BOOL)collectionView:(UICollectionView *)collectionView ...

  5. UIButton在Disabled状态下标题混乱的问题

    最近开发中遇到的问题汇总 有段时间没有归纳开发中遇到的一些问题了,今天就写一下之前开发中遇到的几个问题.希望这 篇文章能让读者在以后的开发中少走弯路.本文将依次介绍<UIButton在Disab ...

  6. iOS MJRefresh设置MJRefreshStateNoMoreData状态图片

    MJRefresh地址 //  代码地址: https://github.com/CoderMJLee/MJRefresh//  代码地址: http://code4app.com/ios/%E5%B ...

  7. swift 取消UIButton选中高亮状态

    objc可以用通过重写setHighlighted方法来达到当按钮选中时的高亮状态 -(void)setHighlighted:(BOOL)highlighted{ } swift中取消高亮状态 ov ...

  8. 设置button不同状态下的背景色,即把这个颜色变成图片设置成,背景图片

    - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state { [self setBack ...

  9. ios UIButton设置单选效果,以及同时设置图片和标题

    一,设置单选效果 - (void)selectedBtnPress:(UIButton*)sender { //首先把原来按钮的选中效果消除 for (int i=0;i<num;i++) {/ ...

随机推荐

  1. hdu 4513 最长不下降回文序列【manacher】

    <题目链接> 吉哥又想出了一个新的完美队形游戏!  假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形 ...

  2. OSINT系列:网站信任评估WOT

     OSINT系列:网站信任评估WOT Web of Trust(WOT)是芬兰的一家网站信任评估服务公司.它通过收集用户对网站的评价,来评估网站的可信任度.在该公司网站www.mywot.com,用户 ...

  3. PHP函数 ------ ctype_alnum

    //判断是否是字母和数字或字母数字的组合 if(!ctype_alnum($str)){ echo '只能是字母或数字的组合';exit; }整理下ctype functions: 1.ctype_a ...

  4. Unity 鼠标个性化

    最初的版本,API是可以直接设置鼠标显示与否的,新版本就改了,先上下旧店的版本的: 1.思路: 在某些游戏下,经常会隐藏鼠标,或者有绚丽的动画来代替鼠标显示. 原理就是将鼠标隐藏(不显示)起来,设置一 ...

  5. web开发必备的浏览器常识

    浏览器内核: 1.使用Trident内核的浏览器:IE.Maxthon.TT.The World等: 2.使用Gecko内核的浏览器:Netcape6及以上版本.FireFox.MozillaSuit ...

  6. 通过ZwQuerySystemInformation获取EPROCESS

    google一下,发现很多都是直接通过ZwQuerySystemInformation通过11号获取进程结构SYSTEM_PROCESS_INFORMATION,对于详细的进程信息表达不够.所以想要通 ...

  7. [Axure RP] – 鼠标滑入按钮时自动下拉表单的设计示例

    转:http://blog.qdac.cc/?p=2197 Axure RP 是个好东东呀,大大方便了程序员与客户之间的前期调研时的交流.不过有一些控制并没有鼠标移入和移出的操作,比如 HTML 按钮 ...

  8. useradd 命令的常见用法

    在Linux系统中 useradd 是个很基本的命令,但是使用起来却很不直观.以至于在 Ubuntu 中居然添加了一个 adduser 命令来简化添加用户的操作.本文主要描述笔者在学习使用 usera ...

  9. [Java] Hashtable 源码简要分析

    Hashtable /HashMap / LinkedHashMap 概述 * Hashtable比较早,是线程安全的哈希映射表.内部采用Entry[]数组,每个Entry均可作为链表的头,用来解决冲 ...

  10. install pymongo,mysql

    yum install pymongo yum install MySQL-python