Sometimes you want to be able to handle touch or clicks on labels, not just buttons.
To add an touch or click event on a label, we must create a UITapGestureRecognizer
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tappedLabel(tapGestureRecognizer:)))
label.addGestureRecognizer(tapGesture)
Then you can create a function to handle the tap.
@objc func tappedLabel(tapGestureRecognizer: UITapGestureRecognizer) {
// do stuff here
}
If that doesn’t work, make sure user interaction is enabled on the label:
label.isUserInteractionEnabled = true
The Complete iOS App Development Bootcamp
Disclosure: This website may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.