I have a view basic UITextField and “submit” button. When the user taps in the UITextField the keyboard appears. I shift the view up so the useful bits aren’t blocked by the keyboard and when they keyboard goes away I shift stuff back down again (registered for keyboard show/hide notifications).
I want users to be able to tap anywhere in the view to ditch the keyboard. So I added a UITapGestureRecognizer and I call resignFirstResponder on my text field. Works great…. mostly.
If you tap the “submit” button it doesn’t work. It’s not cancelsTouchesInView, I set that to NO. It’s the moving view. The “tap” gets registered in the post shift down view. So when I slide the view down 80 points the tap the user made “on the button” winds up being 80 points above the button.
So how do I get that tap passed through before the Gesture Recognizer event fires and causes the view to shift?
I found a fix which works best for me.
UIGestureRecognizerDelegate has a method
– gestureRecognizer:shouldReceiveTouch:which lets you choose say if the gesture recognizer should accept the touch.In essence, if the tap happened on anything which is a sub-class of a UIControl object, let that object handle the click, otherwise my gesture recognizer gets it.