关于ios:UIButton-禁用高亮

47次阅读

共计 1259 个字符,预计需要花费 4 分钟才能阅读完成。

参考: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

正文完
 0