UIButton 禁用高亮
Posted AlexLi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIButton 禁用高亮相关的知识,希望对你有一定的参考价值。
参考:https://www.itranslater.com/qa/details/2129903136516080640
方式一:设置高亮和 Selected 同样(如果有title、image等,也要这样设置,不推荐这个方式)
button.adjustsImageWhenHighlighted = NO;
[button setBackgroundImage:[UIImage imageNamed:@"switch_close"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"switch_open"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:@"switch_open"] forState:UIControlStateSelected | UIControlStateHighlighted];
方式二:UIControlEventAllTouchEvents(推荐)
[button addTarget:self action:@selector(allTouchEventsClick:) forControlEvents:UIControlEventAllTouchEvents];
- (void)allTouchEventsClick:(UIButton *)button{
button.highlighted = NO;
}
Tip:可以创建一个自己的Button,方便使用
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface TMButton : UIButton
/// 如果外面需要监听UIControlEventAllTouchEvents可以使用 block 代替
@property (copy, nonatomic) void(^allTouchEventsClickBlock)(UIButton *button);
@end
NS_ASSUME_NONNULL_END
#import "TMButton.h"
@implementation TMButton
- (instancetype)init
{
self = [super init];
if (self) {
[self addTarget:self action:@selector(allTouchEventsClick:) forControlEvents:UIControlEventAllTouchEvents];
}
return self;
}
- (void)allTouchEventsClick:(UIButton *)button{
button.highlighted = NO;
if(self.allTouchEventsClickBlock){
self.allTouchEventsClickBlock(button);
}
}
@end
以上是关于UIButton 禁用高亮的主要内容,如果未能解决你的问题,请参考以下文章
为 PaintCode 代码创建的 UIButton 添加高亮效果