How to make a UILabel clickable in Swift 5?

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 uninstall Cocoapods from the Mac OS? #4 How to Use SQL MAX() Function with Dates? #5 How to add Read More Read Less Button using JavaScript? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Apr 7 Solution for “Yarn build: Failed because of a stylelint error” Apr 7 Make sure your links have descriptive text Apr 7 How to add Laravel to WordPress using Sage theme (and install Tailwind CSS)? Apr 2 How to create a sidebar using pure CSS? Apr 1 How to switch dark and light themes using pure CSS? Apr 1 How to calculate elapsed time in JavaScript?

You might also like these

5 Ways to Loop Through JavaScript ArraysJavaScriptclip and clip-path properties in CSSCSSHow to get recent posts in WordPress?WordPressHow to switch dark and light themes using pure CSS?CSSUNION and UNION ALL in PostgresPostgresHow to get Current Year, Month and Date in JavaScript?JavaScript