Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add gesture recognizer to UI Label?

enter image description here

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

    }


}
like image 437
Alexa289 Avatar asked Dec 13 '22 18:12

Alexa289


1 Answers

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.)

like image 155
matt Avatar answered Dec 28 '22 15:12

matt