Making a Uitableview Scroll When Text Field Is Selected

Scroll UITextField above Keyboard in a UITableViewCell on a regular UIViewController

I spent all day trying to figure this out. I posted it here, then found a blog link and an incredibly simple solution. It looks like this:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:self.tableView];
CGPoint contentOffset = self.tableView.contentOffset;

contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);

NSLog(@"contentOffset is: %@", NSStringFromCGPoint(contentOffset));

[self.tableView setContentOffset:contentOffset animated:YES];

return YES;
}


-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];

if ([textField.superview.superview isKindOfClass:[UITableViewCell class]])
{
UITableViewCell *cell = (UITableViewCell*)textField.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:TRUE];
}

return YES;
}

Check this for iOS 8

Get UITableView to scroll to the selected UITextField and Avoid Being Hidden by Keyboard

In my app, I have successfully used a combination of contentInset and scrollToRowAtIndexPath like this:

When you want to display the keyboard, just add a contentInset at the bottom with your table with desired height:

tableView.contentInset =  UIEdgeInsetsMake(0, 0, height, 0);

Then, you can safely use

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:cell_index inSection:cell_section] animated:YES];

By adding the contentInset, even if you are focusing on the last cell the tableView will still be able to scroll. Just make sure that when you are dismissing the keyboard, you reset the contentInset.

EDIT:

If you have only one section (you can replace cell_section with 0) and the use the textView tag to inform the cell row.

Not able to set proper UITableView scroll when keyboard is shown and when keyboard resigns

Try this;

-(void)keyboardNotifier:(NSNotification *)notification {
if(keyboardStatus == NO) {
CGRect frame = [RegisterTableVIew frame]; //RegisterTableVIew is tableview
frame.size.height -= 200;
[RegisterTableVIew setFrame:frame];
keyboardStatus = YES;
}
}

-(void) keyboardWillHide:(NSNotification *)note
{
if(keyboardStatus) {
CGRect frame = [RegisterTableVIew frame]; //RegisterTableVIew is tableview
frame.size.height += 200;
[RegisterTableVIew setFrame:frame];
keyboardStatus = NO;
}
}

I answerwed from what i understood from your question that each time you click on textfield, tableView scrolls up creating lot of space.

How can I get a custom UITableView to automatically scroll to a selected text field?

I fixed this by twiddling contentInset on the UITableView when the keyboard appears or disappears.

- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)aNotification {
CGRect keyboardBounds;
[[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
keyboardHeight = keyboardBounds.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);
[UIView commitAnimations];
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[items count] inSection:0]
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
tableView.contentInset = UIEdgeInsetsZero;
[UIView commitAnimations];
}

call registerForKeyboardNotifications when you load the UITableView, and everything else should Just Work.

Scroll UITextField above Keyboard in a UITableViewCell on a regular UIViewController

I spent all day trying to figure this out. I posted it here, then found a blog link and an incredibly simple solution. It looks like this:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:self.tableView];
CGPoint contentOffset = self.tableView.contentOffset;

contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);

NSLog(@"contentOffset is: %@", NSStringFromCGPoint(contentOffset));

[self.tableView setContentOffset:contentOffset animated:YES];

return YES;
}


-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];

if ([textField.superview.superview isKindOfClass:[UITableViewCell class]])
{
UITableViewCell *cell = (UITableViewCell*)textField.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:TRUE];
}

return YES;
}

Check this for iOS 8



Related Topics



Leave a reply



Submit