UIButton在UICollectionViewCell中以编程方式添加并不起作用

时间:2017-08-27 07:05:23

标签: ios swift uiview uibutton uicollectionview

我正在尝试将UIButton添加为UICollectionViewCell中另一个自定义UIView的子视图 然而,我添加的这两个按钮不会响应触摸并且不执行任何操作。

我在这里初始化按钮:

let likeButton: UIButton = {
    let button = UIButton(type: .system)
    button.anchor(width: 40, height: 40)
    button.backgroundColor = UIColor(red: 239.0/255.0, green: 83.0/255.0, blue: 80.0/255.0, alpha: 0.8)
    button.layer.cornerRadius = 20
    button.layer.masksToBounds = true
    button.isUserInteractionEnabled = true
    return button
}()

let commentButton: UIButton = {
    let button = UIButton(type: .system)
    button.backgroundColor = UIColor(red: 38.0/255.0, green: 37.0/255.0, blue: 37.0/255.0, alpha: 0.8)
    button.layer.cornerRadius = 20
    button.layer.masksToBounds = true
    return button
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    likeButton.addTarget(self, action: #selector(handleLike), for: .touchUpInside)
    commentButton.addTarget(self, action: #selector(handleComment), for: .touchUpInside)
    setupViews()
}

func handleLike(_ sender: UIButton) {
    print("like")
}

func handleComment(_ sender: UIButton) {
    print("comment")
}

添加为子视图并设置约束:

    override init(frame: CGRect) {
    super.init(frame: frame)

    setupViews()
}

func setupViews() {

    let stackView = UIStackView(arrangedSubviews: [likesLabel, commentsLabel, viewsLabel])
    stackView.axis = .horizontal
    stackView.distribution = .fillProportionally
    stackView.spacing = 4

    addSubview(newsImage)
    newsImage.addSubview(darkView)
    darkView.addSubview(newsTitleLabel)
    darkView.addSubview(stackView)
    darkView.addSubview(timeStampLabel)
    setButtonsStackView()

    addConstraintsWithFormaat(format: "H:|[v0]|", views: newsImage)
    addConstraintsWithFormaat(format: "V:|[v0]|", views: newsImage)
    newsImage.addConstraintsWithFormaat(format: "H:|[v0]|", views: darkView)
    newsImage.addConstraintsWithFormaat(format: "V:|[v0]|", views: darkView)

    stackView.anchor(top: nil, left: leftAnchor, bottom: bottomAnchor, right: nil, paddingTop: 0, paddingLeft: 10, paddingBottom: -10, paddingRight: 0)
    newsTitleLabel.anchor(top: nil, left: leftAnchor, bottom: stackView.topAnchor, right: nil, paddingTop: 0, paddingLeft: 10, paddingBottom: -5, paddingRight: 0)
    timeStampLabel.anchor(top: nil, left: nil, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: -10, paddingRight: 10)
}

func setButtonsStackView() {
    let buttonStackView = UIStackView(arrangedSubviews: [likeButton, commentButton])
    buttonStackView.axis = .horizontal
    buttonStackView.distribution = .fillProportionally
    buttonStackView.spacing = 10
    darkView.addSubview(buttonStackView)
    buttonStackView.anchor(top: darkView.topAnchor, left: nil, bottom: nil, right: darkView.rightAnchor, paddingTop: 10, paddingLeft: 10, paddingBottom: 0, paddingRight: 10)
}

因为我正在考虑问题可能在于设置约束,而且当我在添加按钮作为UIImageView的子视图时我从另一篇文章中读取时按钮永远不会工作。

无论如何,我很乐意得到任何建议!

PS:我使用自定义扩展来设置约束。他们的工作很好,因为我经常在我的项目中使用它们。

谢谢!

2 个答案:

答案 0 :(得分:0)

你可以尝试,

 newsImage.isUserInteractionEnabled = true
  

默认情况下,UserInteraction的{​​{1}}属性设置为   UIImageView。这是我们大多数人犯错误的地方。所以主要   在向false添加任何控件时要检查的事情是设置   其UIImageView属性为isUserInteractionEnabled

答案 1 :(得分:0)

我刚刚意识到我必须设置与单元格的contentView相关的buttonStackView约束。

    addSubview(buttonStackView)
    buttonStackView.anchor(top: topAnchor, left: nil,
                           bottom: nil, right: rightAnchor,
                           paddingTop: 10, paddingLeft: 10,
                           paddingBottom: 0, paddingRight: 10)

之后,所有按钮都能正常工作并响应触摸。

将按钮设置为UIImageView的子视图实际上是主要问题。