1

Currently I have a UITextField in each UITableViewCell but the issue is, if there are 2 cells or more the cell could be cut off.

Is there any easy way to make it so that the cell is visible while my keyboard is open?

Thanks!

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

2 Answers2

2

That worked pretty well for me, in case somebody still need the answer:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (self.tableView.contentOffset.y == 0)
    {
        [UIView animateWithDuration:0.0 delay:0.5 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        } completion:^(BOOL finished) {
            UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
            [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
        }];
    }
}

The problem was that the keyboard was not taken into consideration when sliding to the cell, so I just added the call in a small delay block.

allaire
  • 5,995
  • 3
  • 41
  • 56
  • Thanks for this! It works for me without the check for contentOffset.y=0. For reasons unknown my UITableView scrolls automatically to show the text field in iOS 6, but needs this help in iOS 5. – Eric Hedstrom Mar 27 '13 at 06:18
1

If i'm understanding your question correctly, you want the view to scroll when the keyboard shows up. If you make your viewController a subclass of UITableViewController, I believe you get scrolling for free. The only other option is unfortunately not that simple. You need to play around with the delegation methods to move your view up and down (and animate it if you want it to slide up with the keyboard). There's lots of answers to this you can find so I won't go into the details, here's a few links to help you out!

In depth explation here

Great explanation here

Some UIScrollView details here

More here

Community
  • 1
  • 1
MGA
  • 3,711
  • 1
  • 21
  • 22
  • I read through those articles but the issue here is that I don't need to move the tableview everytime but only if it is blocking the cell in which my text field is in. – SimplyKiwi Dec 31 '11 at 03:26
  • Yes, that's what most of these is addressing. You don't want to *only* resize the view if it is going to be blocked. You want to resize it every time so that the user can still scroll all the way to the bottom. What you do is determine the offset created by the keyboard, shorten the view by that much, and then scroll the view to the appropriate location if necessary. If the cell is already in the view, you can simply not scroll and leave as is. You can also opt to always move the selected cell to the center of the viewable area, but that is up to you. – MGA Dec 31 '11 at 07:24
  • Instead of doing all this crazy amount of code, I just set the textField to the navigationBar title and did it that way, just as easy! – SimplyKiwi Dec 31 '11 at 17:27