5

I have a UITapGestureRecognizer attached to a UITextField to get a "drop down" like effect. When the UITextField is tapped, I present a UIPopover with the content. This worked like a charm pre 7.1 - Now the UITextField just becomes first responder, and the gesturerecognizer is totally ignored. Tried setting delaysTouchedBegan to YES but it didn't help.Any help?

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
HeineSkov
  • 449
  • 1
  • 7
  • 18

3 Answers3

16

Why to use UITapGestureRecognizer, better to use UITextFieldDelegate methods

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{

   //Do what you need to do...

}

OR

You can wrap up your textView in a UIView and add the UITapGestureRecognizer on that view.

Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
  • 2
    I just wrapped the textview in another view. But need to disable the user interaction of the text field. – Klu Mar 13 '14 at 17:24
  • 2
    The `textFieldShouldBeginEditing:` delegate method won't be called when the text field is already the first responder. By implementing the gesture recognizer delegate as answered by @JuroSheridan it should work correctly in all cases. – Joris Kluivers Feb 09 '16 at 11:13
8

Implement the delegate method for your tap gesture

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
   return YES;
}

Then set yourTapGesture.delegate = self;

  • 1
    This should be the accepted answer. Using textViewShouldBeginEditing has some issues with it. IF you have multiple UITextFields in your view controller, textViewShouldBeginEditing gets called for all the UITextFields at random times. – K.K Jan 10 '17 at 10:42
2

Implement the delegate method of the UITextField:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
      // Show popover here

      return NO;
}
graver
  • 15,183
  • 4
  • 46
  • 62