ios - 在 UICollectionViewCell 中以编程方式添加的 UIButton 不起作用

标签 ios swift uiview uibutton uicollectionview

我正在尝试将 UIButton 添加为 UICollectionViewCell 内另一个自定义 UIView 的 subview 但是,我添加的这两个按钮不会响应触摸,也不执行任何操作。

这里我正在初始化按钮:

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")
}

添加为 subview 并设置约束:

    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 的 subview 时,按钮永远不会工作。

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

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

谢谢!

最佳答案

你可以试试

 newsImage.isUserInteractionEnabled = true

By default the UserInteraction property of UIImageView is set to false. This the place where most of us makes mistake. So the main thing to check in while adding any control to UIImageView is to set its isUserInteractionEnabled property to true.

关于ios - 在 UICollectionViewCell 中以编程方式添加的 UIButton 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45902490/

相关文章:

ios - 为什么当我返回时我的主视图 Controller 不更新?

swift - 我应该如何处理 shouldPerformSegueWithIdentifier 中的 Alamofire 响应?

ios - 如何以编程方式从 UIView 获取约束 "bottomSpace"?

ios - 从没有状态栏的全屏 View 到有状态栏的普通 View 的错误翻转转换

ios - 如何从父 View 中删除 uiview?

ios - 响应 native iOS Spotify SDK

ios - 带有 AVPlayer 的应用程序在启动后播放 mp4 中断 iPod 音乐

objective-c - 如何使用 block 更改委托(delegate)方法?

ios - 旋转后圆寄宿生不圆

swift - swift segue不起作用(初学者)