【问题标题】:UIButton added programatically in UICollectionViewCell doesn't work在 UICollectionViewCell 中以编程方式添加的 UIButton 不起作用
【发布时间】:2018-02-04 17:58:27
【问题描述】:

我正在尝试将 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:我正在使用自定义扩展来设置约束。它们工作得很好,因为我经常在我的项目中使用它们。

谢谢!

【问题讨论】:

    标签: ios swift uiview uibutton uicollectionview


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

      你可以试试,

       newsImage.isUserInteractionEnabled = true
      

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

      【讨论】:

        猜你喜欢
        • 2012-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-04
        • 1970-01-01
        • 2015-04-01
        相关资源
        最近更新 更多