Swipe left gestures over UITextField

I have UITextFields in tableviewcells. When you swipe a cell outside the text box, the delete action will look as expected. If you slide your finger across the text field, it will stop appearing.

How can I fix this so that you can pass through the inputs and the cell activates the delete action?

+6
source share
3 answers

I think the problem here is that touching the text field is interfering with your gesture recognizer (supposedly attached to the parent view). I had a similar problem with a text box placed in a UIScrollView.

I ran into this problem by laying a clean UIView on top of my UITextField. Then I assigned UITapGestureRecognizer to this clear view to set the text field as the first responder when the user clicks on the field. Otherwise, swiped is sent to the parent view (scroll view), which recognizes swipe without any problems. It is a little lame, but it works.

This scenario is a little different from yours, but I think the same problem. Here is what my code looks like, hope this helps:

// UIView subclass header @interface LSAddPageView : UIView @property (weak, nonatomic) IBOutlet UITextField *textField; // Connected to the UITextField in question @property (strong, nonatomic) UIView *textFieldMask; @property (assign, nonatomic) BOOL textFieldMaskEnabled; @end // UIView subclass implementation @implementation LSAddPageView - (void)awakeFromNib { [super awakeFromNib]; _textFieldMask = [UIView new]; _textFieldMask.backgroundColor = [UIColor clearColor]; [self insertSubview:_textFieldMask aboveSubview:self.textField]; } - (void)layoutSubviews { [super layoutSubviews]; _textFieldMask.frame = self.textField.frame; } - (BOOL)textFieldMaskEnabled { return _textFieldMask.hidden == NO; } - (void)setTextFieldMaskEnabled:(BOOL)textFieldMaskEnabled { _textFieldMask.hidden = !textFieldMaskEnabled; } @end 

And then in the controller:

 - (void)viewDidLoad { [super viewDidLoad]; _addPageView = (LSAddPageView*)self.view; _maskGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMask:)]; _maskGestureRecognizer.numberOfTapsRequired = 1; _maskGestureRecognizer.numberOfTouchesRequired = 1; [_addPageView.textFieldMask addGestureRecognizer:_maskGestureRecognizer]; self.textField.delegate = self; // Set delegate to be notified when text field resigns first responder } - (void)didTapMask:(UIGestureRecognizer*)recognizer { _addPageView.textFieldMaskEnabled = NO; [self.textField becomeFirstResponder]; } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { _addPageView.textFieldMaskEnabled = YES; return YES; } 
+2
source

It looks like you need to set the cancelsTouchesInView property

 yourGestureRecognizer.cancelsTouchesInView = NO; 
+1
source

from UIButton and UITextField block UITableViewCell deletion for deletion

 self.tableView.panGestureRecognizer.delaysTouchesBegan = YES; 

this will cause the text box to not stop the left swipe.

+1
source

Source: https://habr.com/ru/post/954644/


All Articles