【发布时间】:2014-02-19 11:17:31
【问题描述】:
我在我的应用程序中对UIButton 进行了子类化,即使我按下按钮,也有很多次高亮颜色保持不变。我无法弄清楚究竟是什么原因造成的,因为它似乎只是偶然发生的,但它似乎发生在大约 50% 的时间里。我很确定这是可重现的。当我在UITableViewCell 中有一个按钮并在表格视图仍在滚动时单击它时,我经常会发生这种情况。
我在子类中覆盖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
【问题讨论】:
标签: ios iphone objective-c ipad uibutton