参考: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
网友评论