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
yourLabel.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(onClickLabel(_:))) self.yourLabel.addGestureRecognizer(tap)
yourLabel.frame = CGRect(x: 0, y: 0, width: 300, height: 20)
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.
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.