首先是adjustsImageWhenHighlighted属性的正确使用:

UIButton的adjustsImageWhenHighlighted属性是当UIButton设置了背景图片时,并且没有设置高亮状态下的背景图片,点击按钮是否有高亮状态。

默认下是YES,也就是说当我们点击按钮的时候会有高亮状态,当我们设置button.adjustsImageWhenHighlighted = NO;时,再点击图片就看不到高亮状态了。

想取消按钮的高亮状态,可以继承UIButton自定义按钮控件,然后在实现文件中重写下面的方法:

// 重写系统setHighlighted方法,取消按钮点击高亮显示
- (void)setHighlighted:(BOOL)highlighted {}

也可以使用KVO,当按钮在高亮状态时可以进行处理,比如:

- (void)addObserver:(UIButton *)button {

    [button addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew context:nil];
} - (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:UIColorFromHEX(0xF8F8F8)];
return;
}
[button setBackgroundColor:UIColorFromHEX(0xFFFFFF)];
}
}

设置按钮高亮状态下的颜色:

[control setBackgroundImage:[UIImage ctRoundRectImageWithFillColor:UIColorFromHEX(0xFAFAFA) cornerRadius:] forState:UIControlStateHighlighted];

也可以使用上面这个方法,模拟出无高亮状态:

[control setBackgroundImage:[UIImage ctRoundRectImageWithFillColor:[UIColor clearColor] cornerRadius:] forState:UIControlStateHighlighted];

上面用到的颜色转Image的方法为:

+ (UIImage *)ctRoundRectImageWithFillColor:(UIColor *)fillColor cornerRadius:(CGFloat)cornerRadius
{
return [self ctRoundRectImageWithFillColor:fillColor borderColor:nil borderWidth:0.0f cornerRadius:cornerRadius];
} + (UIImage *)ctRoundRectImageWithFillColor:(UIColor *)fillColor borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth cornerRadius:(CGFloat)cornerRadius
{
CGFloat halfBorderWidth = borderWidth * 0.5f;
CGFloat w = cornerRadius + halfBorderWidth; CGFloat dw = w * +; UIGraphicsBeginImageContextWithOptions(CGSizeMake(dw, dw), NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext(); UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(halfBorderWidth, halfBorderWidth, dw - borderWidth, dw - borderWidth) cornerRadius:cornerRadius];
[fillColor setFill];
[path fill]; if (borderWidth > 0.0f && borderColor) {
[borderColor setStroke];
path.lineWidth = borderWidth;
[path stroke];
} CGContextAddPath(context, path.CGPath);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return [image resizableImageWithCapInsets:UIEdgeInsetsMake(w+, w+, w+, w+)];
}

UIButton按钮的高亮状态颜色的更多相关文章

  1. 按钮在cell上的高亮状态出现的慢

    在单元格上放一个全屏长的按钮  高度不是cell的高度    当点击cell上的按钮的时候   按钮的高亮状态会出现的比较慢   因为按钮设置的就是touchUpInside   所以当你向下按的时候 ...

  2. 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 {     [ ...

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

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

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

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

  5. UIButton高亮状态卡顿

    童鞋们有么有遇到过一个tableview或者是scrollview上放置一个button然后点击button,但是button的高亮状态切换不过来呢? 解决方案: 新建一个类继承自UITableVie ...

  6. NX二次开发-UFUN获取对象的显示属性(图层,颜色,空白状态,线宽,字体,高亮状态)UF_OBJ_ask_display_properties

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_obj.h> UF_initialize( ...

  7. iOS:UIButton按钮的详解

    UIButton的详细介绍: 一.按钮具有的属性: @property(nonatomic,readonly) UIButtonType buttonType;  //按钮形状类型 @property ...

  8. IOS 修改UIAlertController的按钮标题的字体颜色,字号,内容

    IOS 修改UIAlertController的按钮标题的字体颜色,字号,内容 UIAlertController *alertVC = [UIAlertController alertControl ...

  9. bootstrap按钮加载状态改变

    bootstrap里面有个激活按钮的时候,按钮变成不可用的: 按照官网里面的方法介绍是在button按钮加个data-loading-text="Loading..."属性,然后j ...

随机推荐

  1. 手机内存卡RAW无法格式化的解决办法

    突然出现这个问题,这是麻烦: 网上找了各种办法:什么软件修复,disk等修复,创建新磁盘,新扇到,win自动修复啊:开始----运行框中输入      :convert X: /fs:FAT(X为电脑 ...

  2. JavaScript的流程控制

    JavaScript的流程控制 1.if...else if...else... 2.while 3.for 4.forEach 5.for...in... 完整代码如下: <!DOCTYPE ...

  3. xrdp---远程桌面连接

    xrdp is an Open Source Remote desktop Protocol server, which allows you to RDP to your Linux server ...

  4. 【Python requests多页面爬取案例】

    "```python import requests from fake_useragent import UserAgent # 随机ua库 class Boring(): def __i ...

  5. Whctf 2017 -UNTITLED- Writeup

    Whctf 2017 -UNTITLED- Writeup 转载请表明出处http://www.cnblogs.com/WangAoBo/p/7541481.html 分析: 下载下来的附件是一个py ...

  6. Java对象构成所有Java应用程序的基础

    通过在优锐课的ange交流下,掌握了很多编程思想方法 特来分享 对象具有状态和行为 Java中的对象以及其他任何``面向对象''语言都是所有Java应用程序的基本组成部分,代表了你可能在你周围找到的任 ...

  7. 函数match应打印s中从ch1到ch2之间的所有字符,并且返回ch1的地址。

    1 char *match( char *s, char ch1, char ch2 ){ ; ; ; while(s[len]){ len++; } *len+];//防止s字符串全满导致t溢出 * ...

  8. SQL注入 盲注

    来源:http://www.cnblogs.com/cheatlove/articles/384233.html SQL注入攻击: (1) 脚本注入式的攻击(2) 恶意用户输入用来影响被执行的SQL脚 ...

  9. Bugku - Misc图穷匕见 - Writeup

    Bugku - Misc图穷匕见 - Writeup 原文链接:http://www.cnblogs.com/WangAoBo/p/6950547.html 题目 给了一个jpg图片,下载图片 分析 ...

  10. 计算几何-HPI

    This article is made by Jason-Cow.Welcome to reprint.But please post the article's address.   在线笛卡尔坐 ...