UIButton按钮的高亮状态颜色
2024-08-27 23:06:57
首先是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+)];
}
最新文章
- pt-archiver使用
- 同一个解决方案";引用";其他的项目出现感叹号...
- Xperf Analysis Basics(转)
- python的变量作用域
- Keil 程序调试窗口
- IDA6.6调试安卓程序配置教程
- [Erlang危机](5.0)执行时指标
- CastleWindsor 使用说明
- 开发指南专题六:JEECG微云高速开发平台代码生成
- [Unity优化]批处理04:MaterialPropertyBlock
- edgedb 内部pg 数据存储的探索 (二) 创建数据库命令说明
- visual studio vode 汉化
- Find the Top 10 commands in your linux box!
- 17.泛型.md
- bzoj千题计划292:bzoj2244: [SDOI2011]拦截导弹
- 未能加载文件或程序集&ldquo;Newtonsoft.Json&rdquo;或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
- 原理分析之一:从JDBC到Mybatis
- 埃氏筛法求素数&;构造素数表求素数
- [18/11/30] toString()方法 和 equals() 方法
- Quartz,启动不立即执行问题
热门文章
- python面试的100题(15)
- intermediate-python-for-data-science
- 3.获取某天的最大时间和最小时间,使用Calendar
- Consider using the `--user` option or check the permissions.
- 记录 shell学习过程(5)continue break
- setCascadeOpacityEnabled
- [termux Linux] termux不用ssh远程控制shell
- 深浅拷贝_python
- 题解【洛谷P1807】最长路_NOI导刊2010提高(07)
- mongo shell远程连接使用数据库