1

I'm building an application in Swift 3, so I want to call a function if I click on a particular UILabel, so I'm write this code but not works:

let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
    self.labelTemp.isUserInteractionEnabled = true
    self.labelTemp.addGestureRecognizer(tap)

How can I render UILabel clickable ?

bircastri
  • 2,169
  • 13
  • 50
  • 119

5 Answers5

2

Set user interaction enabled for the UILabel and add the below code in the viewDidLoad()

self.label.isUserInteractionEnabled = true

let tap = UITapGestureRecognizer(target: self, action: #selector(self.labelTapped))
self.label.addGestureRecognizer(tap)

Add the tap action function as below :

@objc func labelTapped(_ gestureRecognizer: UITapGestureRecognizer) {
    print("Label clicked")
}

Please make user that there is no other transparent view overlapping the UILabel in the view. If the UILabel is a part of another view then please make sure that the container View's user interaction is enabled.

Hope this helps.

BhargavR
  • 1,095
  • 7
  • 14
0

Your selector should be an @objc func within self.

<#YourLabel#>.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleLabelTap)))

And when the user taps the label it will trigger:

@objc func handleLabelTap() {
    // handle label tap here    
}
Gustavo Vollbrecht
  • 3,188
  • 2
  • 19
  • 37
0

Please ensure that You have connected the outlet to UILabel because I have created simple demo code by copy-paste your code and it is worked as expected.

override func viewDidLoad() {
    super.viewDidLoad()
    let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction))
    self.labelTemp.isUserInteractionEnabled = true
    self.labelTemp.addGestureRecognizer(tap)
}

@objc func tapFunction() {
    print("tapFunction")
}

I suggest, Please remove UILabel from UIViewController and add it again.

Download sample code

Note: - Please ensure that user-interaction of UILabelis enabled

Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
0

You are lacking a function to trigger when the gesture touch is recognized. You need to add following:

let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction(_:)))
    self.labelTemp.isUserInteractionEnabled = true
    self.labelTemp.addGestureRecognizer(tap)

@objc func tapFunction(_ gestureRecognizer: UITapGestureRecognizer) {
    // handle label tap here    
}
Mukesh Shakya
  • 425
  • 3
  • 9
0
  1. First you need to add Tap Gesture into storyboard enter image description here

  2. Create Action of that particular gesture

    override func viewDidLoad() {

            super.viewDidLoad()
    
            let tapOnLabel = UITapGestureRecognizer(target: self, action: #selector(self.tapGestireAction))
            self.labelTemp.isUserInteractionEnabled = true
            self.labelTemp.addGestureRecognizer(tapOnLabel)
        }
    
    
      @IBAction func tapGestureAction(_ sender: UITapGestureRecognizer) {
    
            //Perform action
        }
    
Deviyani Swami
  • 749
  • 8
  • 17