ios - UIButton 子类突出显示错误(即使在点击或触摸后突出显示仍然存在)

标签 ios iphone objective-c ipad uibutton

我对 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/

相关文章:

当视口(viewport)设置为 user-scalable=no 时,iOS/移动版 safari 仍会缩放?检查辅助功能设置!

ios - UIViewController 的默认自定义过渡

ios - 网站在iOS中的Safari上崩溃

ios - Storyboard中的 View Controller 在哪里启动?

ios - UIView 自动布局 : hierarchy of subviews VS minimum views hierarchy?

objective-c - iOS Blocks - 定义类似 UIView 动画的 block

ios - NSStream 写入套接字不会在 WiFi 关闭时出现错误。为什么?

ios - 如何设置状态栏背景颜色 iOS 7

iphone - 批量向(多个)源文件添加 "-fno-objc-arc"标志

iphone - 在真实 iPhone 上测试应用程序