How To Handle Touch Or Click Events On UILabels In Swift Programmatically

Sometimes you want to be able to handle touch or clicks on labels, not just buttons.

How To Handle Touch Or Click Events On UILabels In Swift Programmatically

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
If you liked this post and want to learn more, check out The Complete iOS Developer Bootcamp. Speed up your learning curve - hundreds of students have already joined. Thanks for reading!

Eddy Chung

I teach iOS development on ZeroToAppStore.com.

Similar Posts