Yogesh Chauhan's Blog

How to make a UILabel clickable in Swift 5?

in Swift on July 16, 2020


@IBOutlet weak var firstLabel: UILabel!

override func viewDidLoad() {

  let tap = UITapGestureRecognizer(target: self, action: #selector(self.firstLabelTap(sender:)))
  firstLabel.isUserInteractionEnabled = true

@objc func firstLabelTap(sender:UITapGestureRecognizer) {

  // tap works
  // add your logic


We can use brief taps on the screen to implement button-like interactions with any kind of content.

Tap gestures detect one or more fingers touching the screen briefly.

The fingers involved in these gestures must not move significantly from the initial touch points, and you can configure the number of times the fingers must touch the screen.

For example, you might configure tap gesture recognizers to detect single taps, double taps, or triple taps.

You can attach a gesture recognizer in one of these ways:

1. Programmatically. Call the addGestureRecognizer(_:) method of your view.

2. In Interface Builder. Drag the appropriate object from the library and drop it onto your view.

A UITapGestureRecognizer object provides event handling capabilities similar to those of a button—it detects a tap in its view and reports that tap to your action method.


It’s concrete subclass of UIGestureRecognizer that looks for single or multiple taps.


class UITapGestureRecognizer : UIGestureRecognizer


For the gesture to be recognized, the specified number of fingers must tap the view a specified number of times.

Although taps are discrete gestures, they are discrete for each state of the gesture recognizer; thus the associated action message is sent when the gesture begins and is sent for each intermediate state until (and including) the ending state of the gesture.

Code that handles tap gestures should therefore test for the state of the gesture, for example:

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == .ended {
        // handling code

Action methods handling this gesture may get the location of the gesture as a whole by calling the UIGestureRecognizer method location(in:); if there are multiple taps, this location is the first tap; if there are multiple touches, this location is the centroid of all fingers tapping the view.

Clients may get the location of particular touches in the tap by calling location(ofTouch:in:); if multiple taps are allowed, this location is that of the first tap.

Tap gestures are discrete, so your action method is called only when the tap gesture is recognized successfully.

You can configure a tap gesture recognizer to require any number of taps—for example, single taps or double taps—before your action method is called.

Always check the gesture recognizer’s state property before taking any actions, even for a discrete gesture recognizer.

If the code for your tap gesture recognizer is not called, check to see if the following conditions are true, and make corrections as needed:

1. The isUserInteractionEnabled property of the view is set to true. Image views and labels set this property to false by default. (I HAVE USED IT IN THE SOLUTION ABOVE.)

2. The number of taps was equal to the number specified in the numberOfTapsRequired property.

3. The number of fingers was equal to the number specified in the numberOfTouchesRequired property.


It attaches a gesture recognizer to the view.


func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer)

Attaching a gesture recognizer to a view defines the scope of the represented gesture, causing it to receive touches hit-tested to that view and all of its subviews.

The view establishes a strong reference to the gesture recognizer.


It’s a Boolean value that determines whether user events are ignored and removed from the event queue.


var isUserInteractionEnabled: Bool { get set }

When set to false, touch, press, keyboard, and focus events intended for the view are ignored and removed from the event queue.

When set to true, events are delivered to the view normally. The default value of this property is true.

During an animation, user interactions are temporarily disabled for all views involved in the animation, regardless of the value in this property.

You can disable this behavior by specifying the allowUserInteraction option when configuring the animation.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Check if mixin is being called in a style rule or not in SCSSSCSSCSS backface-visibility PropertyCSS2 Ways we can create an Array in JavaScriptJavaScriptAdd animated hamburgers menu using Hamburgers collection on GitHubCSSWhat is the best way to add JavaScript Code into HTML?HTMLadd_filter function in WordPressWordPress