【问题标题】:UIButton Not Going to TargetUIButton 不会指向目标
【发布时间】:2020-04-18 20:37:41
【问题描述】:

我有一个 UICollectionViewController 用作我的主要 VC,然后一个 UICollectionViewCell 设置为顶部作为标题。但是当我尝试按下单元格内的按钮时,我无法得到回应。我正在为我的 viewDidLoad 使用 initWithFrame。此单元格上的所有按钮都是这种情况。有什么想法吗?

来自 UICollectionViewCell 或标题的代码

let editProfileFollowButton: UIButton = {
            let button = UIButton(type: .system)
            button.setTitle("Loading", for: .normal)
            button.layer.cornerRadius = 3
            button.layer.borderColor = UIColor.lightGray.cgColor
            button.layer.borderWidth = 0.5
            button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
            button.setTitleColor(.black, for: .normal)
            button.isEnabled = true
            button.addTarget(self, action: #selector(handleButtonFunction), for: .touchUpInside)
            return button
        }()

    @objc func handleButtonFunction() {
            print("button clicked")
        }

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

            addSubview(editProfileFollowButton)
            editProfileFollowButton.anchor(top: postLabel.bottomAnchor, left: postLabel.leftAnchor, bottom: nil, right: self.rightAnchor, paddingTop: 12, paddingLeft: 12, paddingBottom: 0, paddingRight: 20, width: 0, height: 30)


        }

【问题讨论】:

  • 您是否禁用了单元格选择 (allowsSelection = no)?
  • @MAT - 不,我没有向单元格或 CollectionView 添加任何禁用行。
  • 您是否获得了按钮点击效果(按下)?
  • @Kamran 是的,它像我点击它一样闪烁,但我没有从目标获得打印语句。
  • @ShawnaBoucher 看起来很奇怪。至少它应该打印button clicked 语句。我建议在 github 上的某处分享完整的文件或示例示例。

标签: swift uibutton uicollectionviewcell ios13


【解决方案1】:

您可以尝试两种选择。首先,尝试cell.bringSubviewToFront(cell.button)。如果这不起作用,请尝试cell.contentView.isUserInteractionEnabled = false

【讨论】:

  • 我将这两行都添加到 CollectionView 中(这个函数: override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { )但都没有工作。对于 cell.bringSubViewToFront - 你有 cell.button 的地方,我放了单元格视图。不确定这是否正确。
  • collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: 中尝试cell.contentView.isUserInteractionEnabled = false
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多