【问题标题】:Programmatic UIButton as a subview of UIView is not responding作为 UIView 的子视图的程序化 UIButton 没有响应
【发布时间】:2019-11-26 18:05:16
【问题描述】:

我正在创建一个扩展 UITableViewCell 的类——它将显示一个社交媒体新闻源,每个单元格都是某人的帖子。

我添加了一个 UIButton 作为 UIView 的子视图,因此 UIView 将包含按钮和标签作为子视图,like this。 (抱歉图片有点小,但我想你会明白的)。

但是,UIButton 没有响应点击,甚至没有突出显示。它的superview本身有很多superview,都是栈,但是我查了一下,每个superview的isUserInteractionEnabled都是真的。

    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button(UIImage(named: "vertical_dots")?.withRenderingMode(.alwaysOriginal), for: .normal)
    button.backgroundColor = .white
    button.imageView?.contentMode = .scaleAspectFit
    button.addTarget(self, action: #selector(myButtonAction), for: .touchUpInside)
    
    label.text = "\(userPost?.minutesAgo)m"
    label.font = label.font.withSize(11.0)
    label.textColor = .lightGray
    label.translatesAutoresizingMaskIntoConstraints = false
    label.numberOfLines = 1
    
    let superview = UIView()
    superview.translatesAutoresizingMaskIntoConstraints = false
    superview.addSubview(button)
    superview.addSubview(label)
    
    label.leftAnchor.constraint(equalTo: superview.leftAnchor).isActive = true
    label.heightAnchor.constraint(equalToConstant: 40).isActive = true
    label.centerYAnchor.constraint(equalTo: superview.centerYAnchor).isActive = true
    button.leftAnchor.constraint(equalTo: label.rightAnchor).isActive = true
    button.centerYAnchor.constraint(equalTo: superview.centerYAnchor).isActive = true
    button.heightAnchor.constraint(equalToConstant: 20).isActive = true
    button.widthAnchor.constraint(equalToConstant: 15).isActive = true
    

我收到了很多约束警告,但我认为这不会影响事情吗?

请注意,这只是我的代码的一部分,我正在将超级视图添加到其他视图中,但我没有在这里展示所有内容。我的所有视图都没有框架,只有约束,而且它们都显示正确。

我认为以前没有人问过这个问题,因为其他人正在使用 Interface Builder,而我正在以编程方式完成这一切。

【问题讨论】:

  • 我的约束不会覆盖它吗?
  • 您还没有添加子视图来查看
  • 我稍后会在代码中执行此操作,但不会在此处显示,我会将该注释添加到我的帖子中
  • 是的,我注意到关于超级视图,它没有框架,但视图仍然在应用程序中正确显示。我的观点都没有我为它们设置的约束的框架——这很糟糕吗?它们都按照我的意愿出现
  • 您将超级视图添加到 TableViewCell 并为其设置约束。你现在还看到按钮和标签吗?我想问题应该是那个

标签: ios swift uibutton subview


【解决方案1】:

根据您显示的限制,您无法点击按钮,因为它超出了superview 的范围。

要确认这一点,请添加以下行:

superview.clipsToBounds = true

而且您几乎肯定不会看到您的按钮或标签。

您需要给superview 宽度和高度约束,或者您需要添加约束以便子视图确定父视图的边界。

例如,添加这些行:

    label.topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
    label.bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
    button.rightAnchor.constraint(equalTo: superview.rightAnchor).isActive = true

【讨论】:

  • 解决了!非常感谢。
【解决方案2】:

可能是表格视图单元格认为它正在被选中并正在取消按钮的触摸。

试试这个:

tableView.allowsSelection = false

如果不是这样,请尝试检查按钮的框架,看看您是否真的在点击它。你可以use the Debug View Hierarchy button查看这个。

另外,你可以试试:

button.clipSubviews = true

如果您看不到按钮,则框架错误,您无法在按钮框架之外点按。 (即使你能看到它。)

【讨论】:

  • 嗯,我试过了,但我仍然得到相同的结果。不过我很欣赏这个建议。
  • 您确定按钮的 isUserInteractionEnabled 设置为 true 吗?
  • 也许检查按钮的框架以确保您确实在点击它? (而不是可能超出按钮范围的图像。)
  • 是的,我查过了,确实如此。而且所有的superviews也是真的
  • 我认为这可能是问题所在,我没有给它一个框架。但是当我为框架分配一个 CGRect 时,我仍然得到相同的结果。我的猜测是我的约束导致了问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-18
  • 2019-03-30
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多