Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swipe left gestures over UITextField

I have UITextFields in tableviewcells. When you swipe over the cell not part of the textfield, the delete action comes up as expected. If you swipe over the textfield, it stops the delete from popping up.

How do I fix this so that you can swipe over the inputs and the cell will trigger the delete action?

like image 582
Stefan Kendall Avatar asked Sep 25 '13 22:09

Stefan Kendall


3 Answers

I think the issue here is that the touch on the text field is interfering with your swipe gesture recognizer (presumably attached to the parent view). I had a similar problem with a text field that was placed in a UIScrollView.

I got around this problem by overlaying a clear UIView on top of my UITextField. I then assigned a UITapGestureRecognizer to this clear view to set the text field as the first responder when the user taps on the field. Otherwise, the swiped is sent to the parent view (a scroll view) which recognizes the swipe without any issues. It's kind of lame but it works.

This scenario is a bit different from yours, but I think it's the same problem. Here is what my code looks like, hopefully it 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;
}
like image 179
Sebastien Martin Avatar answered Sep 19 '22 17:09

Sebastien Martin


Sounds like you need to set the cancelsTouchesInView property

yourGestureRecognizer.cancelsTouchesInView = NO;
like image 44
Tommy Devoy Avatar answered Sep 20 '22 17:09

Tommy Devoy


from UIButton & UITextField will block UITableViewCell being swipe to delete

self.tableView.panGestureRecognizer.delaysTouchesBegan = YES;

this will make the textfield not stop left swipe guestures

like image 37
Austin Haws Avatar answered Sep 21 '22 17:09

Austin Haws