I am trying to add tap gesture to the February label like the picture above, but I don't know why when I add gesture recognizer to the UI Label, it doesn't work, but if I assign that tap gesture to the view (the blue box) , it worked as I am expected.
can we actually add gesture recognizer to UI Label? or what should I do to add gesture to the UI label?
class AttendanceListVC: UIViewController {
@IBOutlet weak var dummyView: UIView!
@IBOutlet weak var monthLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
prepareTapGestureToChooseMonth()
}
}
func prepareTapGestureToChooseMonth() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(AttendanceListVC.tapFunction(sender:)))
tapGesture.numberOfTapsRequired = 1
monthLabel.addGestureRecognizer(tapGesture)
dummyView.addGestureRecognizer(tapGesture)
}
@objc func tapFunction(sender: UITapGestureRecognizer) {
// show pop up
}
}
can we actually add gesture recognizer to UI Label
Certainly. But you would also need to turn on the label's isInteractionEnabled
if you want the gesture recognizer to do anything.
monthLabel.addGestureRecognizer(tapGesture)
monthLabel.isUserInteractionEnabled = true
(Be warned, however, that this may not be a good user interface. Users will not expect to be able to tap a label, and it doesn't respond in a lively visible way to being tapped. Perhaps "February" in your interface should be the title of a button.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With