I'm using a gesture recognizer like this solution to hide the keyboard if the user taps outside of one of my textfields. In my case I'm using a scroll view with some textfields on top. There is also some auto-scroll feature implemented (that's why I'm using the scroll view).
I can hide the keyboard if the user taps outside of it. This is working. I have also enabled the clear button (x button on the right) of the textfield. If the user taps on it the keyboard is hidden, but the content of the textfield is not cleared. Normally I would expect that the content is cleared and the keyboard is not dismissed in this case. This problem was also found by Patrick.
I tried to get the tapped object of the UITapGestureRecognizer
, but this seems to be the UIScrollView
. How can I get the clear button and the auto keyboard hiding feature working?
A generic solution would be nice that would work for all textfields. To complete my question I add my code (which is in C#):
UITapGestureRecognizer tapGesture = new UITapGestureRecognizer ();
tapGesture.CancelsTouchesInView = false;
tapGesture.AddTarget (() => HandleSingleTap (tapGesture));
this.scrollView.AddGestureRecognizer (tapGesture);
private void HandleSingleTap(UITapGestureRecognizer recognizer){
this.scrollView.EndEditing(true);
}
You can of course provide your solutions for Objective-C.