How to connect label and tap gesture?

52 views Asked by At

I made a label and used tapgesturerecogniser to make it work on tap. But when I click on the label, it does not work, however label is being displayed just fine. I am adding them in a scrollview.

Below is my code:

tryLabel.translatesAutoresizingMaskIntoConstraints = false
                            tryLabel.textColor = .white
                            tryLabel.text = "tryLabel"
                            tryLabel.textAlignment = .left
                            tryLabel.isUserInteractionEnabled = true
                            let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(dateAddedTapped))
                            tapGesture.numberOfTapsRequired = 1
                            tapGesture.isEnabled = true
                            tryLabel.gestureRecognizerShouldBegin(tapGesture)
                            editInfoView.addSubview(tryLabel)

                            tryLabel.leftAnchor.constraint(equalTo: editInfoView.leftAnchor, constant: 310).isActive = true
                            tryLabel.topAnchor.constraint(equalTo: editInfoView.topAnchor, constant: 150).isActive = true
                            tryLabel.widthAnchor.constraint(equalToConstant: 300).isActive = true
                            tryLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true

@objc func dateAddedTapped(){print(" Date added ")}
0

There are 0 answers