首页 > 解决方案 > UIButton 不会指向目标

问题描述

我有一个 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)


        }

标签: swiftuibuttonuicollectionviewcellios13

解决方案


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


推荐阅读