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

By Eddy Chung

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, you'll love my free guide: Secrets To iOS Development. Speed up your learning curve - hundreds of students have already downloaded. Thanks for reading!
Profile Picture of Eddy Chung

Eddy Chung

I am a professional iOS developer in Silicon Valley. I teach iOS development on ZeroToAppStore.com. If you'd like to learn more about me click here or you can contact me at: eddy@zerotoappstore.com

Similar Posts