简体   繁体   中英

Tap gesture recognizer added to UILabel not working

I've the following code to add a gesture recognizer to a UILabel. User Interaction Enabled is ticked on for the label in the storyboard, but when I tap on the label the onUserClickingSendToken method is not being called.

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

    @IBOutlet weak var tokenDisplay: UILabel!
    var tapGestureRecognizer:UITapGestureRecognizer = UITapGestureRecognizer(target:self, action:  #selector(onUserClickingSendToken(_:)))

    override func viewDidLoad() {
        super.viewDidLoad()
        tapGestureRecognizer.numberOfTapsRequired = 1
        tokenDisplay.addGestureRecognizer(tapGestureRecognizer)
    }

    func onUserClickingSendToken(_ sender: Any)
    {
      ....

Initializing the tapRecognizer in viewDidLoad should do it, cause you were targeting self before the view was initialized

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

@IBOutlet weak var tokenDisplay: UILabel!
var tapGestureRecognizer:UITapGestureRecognizer!

override func viewDidLoad() {
    super.viewDidLoad()
    tapGestureRecognizer = UITapGestureRecognizer(target:self, action:      #selector(onUserClickingSendToken(_:)))
    tapGestureRecognizer.numberOfTapsRequired = 1
    tokenDisplay.isUserInteractionEnabled = true
    tokenDisplay.addGestureRecognizer(tapGestureRecognizer)
}

@objc func onUserClickingSendToken(_ sender: Any)
{
  ....

Try changing your selector setup to:

#selector(ViewController .onUserClickingSendToken(_:) .

In general for UILabel clickable issue:

There are couple of reasons why your UILabel will not work as clickable

  1. Make sure to mark UILabel UserInteraction true and if yourLabel is inside other view than mark that view UserInteraction true too.

yourLabel.isUserInteractionEnabled = true

  1. Assigning self to target before the view initialisation, move your code inside viewDidLoad/awakeFromNib or after view Load

let tap = UITapGestureRecognizer(target: self, action: #selector(onClickLabel(_:))) self.yourLabel.addGestureRecognizer(tap)

  1. If you are adding UILabel programmatically, than you have to assign it's frame too.

yourLabel.frame = CGRect(x: 0, y: 0, width: 300, height: 20)

  1. Super view in which your UILabel is should be large enough to fit/display label properly, if your label is inside some view than that view's height/weight should assign in a way that it cover the whole UILabel frame.

In my case this was the problem I had, I tried so many thing but at last it turns out that the view in which I had my label had fixed width, the label was displaying properly but onClick event was't working due to out of frame.

在此处输入图像描述

It was missing UILabel's trailing constraints, so half of the thing is not clickable.

  1. Using same tab gesture for multiple view, you cannot use one tab gesture for more than one view or labels,use different for each UILabel.

    try this:

     let tap = UITapGestureRecognizer(target: self, action: #selector(onClickLabel(_:))) self.yourLabel.isUserInteractionEnabled = true self.yourLabel.addGestureRecognizer(tap) let tap2 = UITapGestureRecognizer(target: self, action: #selector(onClickLabel(_:))) self.someView.isUserInteractionEnabled = true self.someView.addGestureRecognizer(tap2)

    instead of this:

     let tap = UITapGestureRecognizer(target: self, action: #selector(onClickLabel(_:))) self.yourLabel.isUserInteractionEnabled = true self.yourLabel.addGestureRecognizer(tap) self.someView.isUserInteractionEnabled = true self.someView.addGestureRecognizer(tap) // tapGesture will work on only one element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM