首先是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. opencv3.2.0+opencv_contrib-3.2.0+vs2015相关文件的配置

    包含目录:E:\opencvcontrib\opencv\sources\build\install\include\opencv E:\opencvcontrib\opencv\sources\bu ...

  2. python3爬虫(4)各种网站视频下载方法

    python3爬虫(4)各种网站视频下载方法原创H-KING 最后发布于2019-01-09 11:06:23 阅读数 13608 收藏展开理论上来讲只要是网上(浏览器)能看到图片,音频,视频,都能够 ...

  3. C++——简单程序设计

    1.一个简单的程序 #include <iostream> //iostream是头文件,用来说明要使用的对象的相关信息. using namespace std; //使用命名空间,解决 ...

  4. (转)GC ROOTS

    还是英文的技术博客更给力,更清楚,本人懒,没有翻译. In your specific example, in any managed environment, Person is not a GC ...

  5. php核心技术与最佳实践 --- 错误与异常

    <?php /*php error*/ /* * 异常和错误的概念不一样 * 在PHP里,遇到任何自身错误都会触发一个错误,而不是抛出异常(对于一些情况,会同时抛出异常和错误) * 异常处理机制 ...

  6. [NOIP2018(PJ)] 摆渡车

    题目链接 题意 有 $n$ 个同学在等车,每位同学从某时刻开始等车,相邻两趟车之间至少间隔 $m$ 分钟.凯凯可以任意安排发车时间,求所有同学等车时间之和的最小值. 分析 这题首先能想到是动态规划 很 ...

  7. 关于layui的日期和时间组件laydate闪屏的坑

    https://blog.csdn.net/liangwenli_/article/details/82786713 jsp页面: <input type="text" cl ...

  8. 找到所有的txt文件并删除

    1.find /oldboy/ -type f -name "*.txt" -delete 2.find /oldboy/ -type f -name "*.txt&qu ...

  9. DFT计算过程详解

    DFT计算过程详解 平时工作中,我们在计算傅里叶变换时,通常会直接调用Matlab中的FFT函数,或者是其他编程语言中已经为我们封装好的函数,很少去探究具体的计算过程,本文以一个具体的例子,向你一步一 ...

  10. Linux - cron - cron 表达式

    概述 之前 cron 的补充 这次介绍下 表达式 背景 之前有说过 cron 这次说下 表达式 1. 准备 环境 os centos7 2. 模板文件 文件 位置 /etc/crontab 作用 系统 ...