'UITapGestureRecognizer on UIDatePicker not working in iOS 15
I have a row for a user to select a date, and I'm using the UIDatePicker
to do this. When a user taps on it, I want it to change some text on the row as well as open the date picker. This was previously working fine with a UITapGestureRecognizer
on my UIDatePicker
that could activate some code, but something broke it and now my UITapGestureRecognizer
is not being recognized at all!
I think it may be iOS 15 that changed something, but looking at the docs I don't see anything really.
Here's what my code is like:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hidePlaceholderLabel))
datePicker.addGestureRecognizer(tapGesture)
My hidePlaceholderLabel
function is never run. How do I bring this functionality back without creating a custom date picker?
I'm using autolayout and have confirmed via Reveal that my date picker does have a frame and should be tappable.
Solution 1:[1]
Add a target for .editingDidBegin
event on the UIDatePicker
. This is called when user taps on UIDatePicker elements in order to adjust the date. For me this works very well
datePicker.addTarget(self, action: #selector(hidePlaceholderLabel), for: .editingDidBegin)
Solution 2:[2]
UIDatePicker inherits from UIControl, just like UIButton. So instead of adding a tap gesture recognizer, you must add an addTarget
method:
datePicker.addTarget(self, action: #selector(hidePlaceholderLabel), for: .touchUpInside)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mircea Dragota |
Solution 2 | Tadreik |