cannyboy
cannyboy

Reputation: 24416

UITextField blocks scrolling within UITableViewCell

I've got UITextFields within UITableViewCells. The text views seems to be blocking the scrolling. When I put my finger-tip within the text view's bound and try to scroll the whole table, the it won't scroll. Scrolling is fine outside the text views.

How can I stop this behavior?

Upvotes: 2

Views: 669

Answers (2)

Antonio MG
Antonio MG

Reputation: 20410

It seems that the only way to avoid this is subclassing the UIScrollView and implement

(BOOL)touchesShouldCancelInContentView:(UIView *)view

Then, detect that the view is a UITextField and return YES, else return NO.

Upvotes: 1

marchinram
marchinram

Reputation: 5888

This code below is a little hacky but it works for me, basically just set userInteractionEnabled on the UITextView to NO then set to YES when you detect the user has tapped it. Below is code for my custom cell with the textview, it scrolls fine for me when user initiates scroll from within UITextView. This works cause the scroll is a pan gesture not a tap.

#import "MyCell.h"

@interface MyCell () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

@implementation MyCell

- (void)awakeFromNib
{
    self.textField.userInteractionEnabled = NO;
    self.textField.delegate = self;

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
    [self addGestureRecognizer:tapRecognizer];
}

#pragma mark - Private methods

- (void)didTap:(UITapGestureRecognizer *)tapRecognizer
{
    CGPoint location = [tapRecognizer locationInView:self];

    CGRect pointRect = CGRectMake(location.x, location.y, 1.0f, 1.0f);
    if (!CGRectIsNull(CGRectIntersection(pointRect, self.textField.frame))) {
        self.textField.userInteractionEnabled = YES;
        [self.textField becomeFirstResponder];
    } 
}

#pragma mark - UITextFieldDelegate methods

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.textField.userInteractionEnabled = NO;
}

@end

Upvotes: 1

Related Questions