UIButton не собирается целиться - PullRequest
0 голосов
/ 18 апреля 2020

У меня есть UICollectionViewController, используемый в качестве основного V C, а затем UICollectionViewCell, установленный вверху в качестве заголовка. Но когда я пытаюсь нажать кнопку внутри ячейки, я не могу получить ответ. Я использую initWithFrame для моего viewDidLoad. Это касается всех кнопок в этой ячейке. Какие-нибудь мысли?

Код из 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)


        }

1 Ответ

0 голосов
/ 19 апреля 2020

Есть два варианта, которые вы можете попробовать. Сначала попробуйте cell.bringSubviewToFront(cell.button). Если это не сработает, попробуйте cell.contentView.isUserInteractionEnabled = false.

...