首先是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. 数组的concat连接

    let arr1 = [1,3,5],arr2 = [2,32,78],arr3 = [];arr3 = arr1.concat(arr2);// arr1 = [1,3,5] arr2 = [2,3 ...

  2. [object object]

    第一个object代表用户自定义的对象的属性. 第二个object代表用户自定义的对象的方法. 是valueOf返回的一个字符串另外你打错了吧应该是[object Object]表示对象的类型是obj ...

  3. vue 3.0 项目搭建移动端 (七) 安装Vant

    # 通过 npm 安装 npm i vant -S 安装完配置 babel.config.js module.exports = { presets: ['@vue/app'], plugins: [ ...

  4. memcached和redis对比

    关于memcached和redis的使用场景,总结如下:两者对比: redis提供数据持久化功能,memcached无持久化. redis的数据结构比memcached要丰富,能完成场景以外的事情: ...

  5. protobuf-net简单使用

    第一个测试的proto文件 syntax = "proto3"; package ProtoMsg; message Foo { ; int32 id = ; repeated b ...

  6. IntelliJ IDEA 2017.3尚硅谷-----缓存和索引的清理

  7. kali linux2019.4安装启动后中文乱码

    1.鼠标右键找到黑框框打开终端 2.终端执行后重启,乱码解决. sudo apt-get install ttf-wqy-zenhei

  8. L2-2 小字辈

    思路 bfs搜一下. 代码 #include <bits/stdc++.h> using namespace std; const int maxn=1e5+10; vector<i ...

  9. OI记录

    这里是蒟蒻xsl的OI记录. 2017 2017.03.?? 开始接触OI 2017.10.14 参加NOIP2017普及组初赛,踩着分数线进入了复赛 2017.11.11 参加NOIP2017普及组 ...

  10. node 崩 处理

    node_modules->bin webpack-dev-server.cmd @IF EXIST "%~dp0\node.exe" ( "%~dp0\node. ...