我对 UIButton
进行了子类化在我的应用程序中,即使我按下按钮,高亮颜色也有很多次保持不变。我无法弄清楚究竟是什么原因造成的,因为它似乎只是偶然发生的,但它似乎发生在大约 50% 的时间里。我很确定这是可重现的。当我在 UITableViewCell
中有一个按钮时,我经常会发生这种情况。我在表格 View 仍在滚动时单击它。
我覆盖 setHighlighted
的方式有问题吗?子类中的方法?这是我的实现:
@implementation SCPFormButton
- (id)initWithFrame:(CGRect)frame label:(NSString *)label
{
self = [super initWithFrame:frame];
if (self) {
UILabel *buttonLabel = [[UILabel alloc] init];
buttonLabel.attributedText = [[NSAttributedString alloc] initWithString:[label uppercaseString] attributes:kButtonLabelAttributes];
[buttonLabel sizeToFit];
buttonLabel.frame = CGRectMake(kMaxWidth / 2 - buttonLabel.frame.size.width / 2, kStandardComponentHeight / 2 - buttonLabel.frame.size.height / 2, buttonLabel.frame.size.width, buttonLabel.frame.size.height);
[self addSubview:buttonLabel];
self.backgroundColor = kFormButtonColorDefault;
}
return self;
}
- (void)setHighlighted:(BOOL)highlighted
{
self.backgroundColor = highlighted ? kFormButtonColorHighlighted : kFormButtonColorDefault;
[self setNeedsDisplay];
}
@end
最佳答案
我会尝试调用super
在您的 setHighlighted
覆盖。确实,Apple docs for UIControl状态:
Specify YES if the control is highlighted; otherwise NO. By default, a control is not highlighted. UIControl automatically sets and clears this state automatically when a touch enters and exits during tracking and when there is a touch up.
因此,
UIControl
中似乎正在进行某种状态处理。与此相关。如果这没有帮助,我会尝试添加一个日志跟踪,以便您可以在处理触摸时检查按钮处于哪个状态。
关于ios - UIButton 子类突出显示错误(即使在点击或触摸后突出显示仍然存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21400832/