【问题标题】:UIButton not clickable when adding it programmatically以编程方式添加 UIButton 时无法点击
【发布时间】:2018-05-05 06:34:12
【问题描述】:

我正在创建自定义 UIView,其中包含两个 UILabels 和 UIButton,因此我创建了一个子类来实现这一点。

现在流程如下:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor whiteColor];

    self.settingsButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width - 2 * margin, frame.size.height - titleHeight - dateHeight, buttonSize, buttonSize)];
    [self.settingsButton setImage:[UIImage imageNamed:@"settings_icon"] forState:UIControlStateNormal];
    [self addSubview:self.settingsButton];

    [self.settingsButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    return self;
}

按钮出现在 UIView 上,但不可点击。我尝试设置self.userInteractionEnabled = YES; 和一些东西但仍然没有结果:(

【问题讨论】:

  • 检查developer.apple.com/library/content/documentation/…是否没有阻碍触摸的视图?
  • 这看起来不错。其余的看起来如何?
  • 确保按钮不在 UIView 的范围之外。简单测试:设置clipsToBounds=YES,看看按钮是否仍然可见
  • @Larme @Wasserfloh 这个测试和调试器的测试显示按钮在UIView 的范围内所以我现在不知道
  • 响应@Larme 的评论,按调试控制台上的View Hierarchy Debug 按钮,看看按钮顶部是否有其他视图

标签: ios objective-c iphone uiview uibutton


【解决方案1】:

这是我犯过的最愚蠢的错误。透明的UINavigationBar 挡住了触摸。

【讨论】:

    猜你喜欢
    • 2014-11-21
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多