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: I don't know exactly how you have your content, but I guess you have only one section (you can replace cell_section with 1) and the cell row is a tag in the text view (you can replace cell_index with textView.

Tag ). It all depends on your code.

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: I don't know exactly how you have your content, but I guess you have only one section (you can replace cell_section with 1) and the cell row is a tag in the text view (you can replace cell_index with textView. Tag). It all depends on your code.

Do you mean: tableView. ContentInset = UIEdgeInsetsMake(0, 0, 0, height); – Lauren Quantrell Mar 10 at 22:48 Sorry about that, my mistake. I was typing this from my memory.

I've edited my post to correct this – Andrei Stanescu Mar 10 at 23:35 The good news is now when I use: wordsTableView scrollToRowAtIndexPath:NSIndexPath indexPathForRow:5 inSection:0 atScrollPosition:UITableViewScrollPositionTop animated:YES; I do indeed get the tableView to scroll to row 5. But what I need is to figure out which row is being invoked. – Lauren Quantrell Mar 11 at 1:30 1 I think you mean UIEdgeInsetsMake(0, 0, height, 0), but otherwise an extremely elegant solution!

Kudos! – Keller Oct 6 at 4:54.

For the sake of anyone else running into this issue, I'm posting the necessary methods here: - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = NSString stringWithFormat: @"%d:%d", indexPath indexAtPosition: 0, indexPath indexAtPosition:1; UITableViewCell *cell = aTableView dequeueReusableCellWithIdentifier:identifier; if (cell == nil) { cell = UITableViewCell alloc initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier autorelease; UITextField *theTextField = UITextField alloc initWithFrame:CGRectMake(180, 10, 130, 25); theTextField. KeyboardType = UIKeyboardTypeDefault; theTextField. ReturnKeyType = UIReturnKeyDone; theTextField.

ClearsOnBeginEditing = NO; theTextField. TextAlignment = UITextAlignmentLeft; // (The tag by indexPath. Row is the critical part to identifying the appropriate // row in textFieldDidBeginEditing and textFieldShouldEndEditing below:) theTextField.

Tag=indexPath. Row; theTextField. Delegate = self; theTextField.

ClearButtonMode = UITextFieldViewModeWhileEditing; theTextField setEnabled: YES; cell addSubview:theTextField; theTextField release; } return cell; } -(void) textFieldDidBeginEditing:(UITextField *)textField { int z = textField. Tag; if (z > 4) { // Only deal with the table row if the row index is 5 // or greater since the first five rows are already // visible above the keyboard // resize the UITableView to fit above the keyboard self.wordsTableView. Frame = CGRectMake(0.0,44.0,320.0,200.0); // adjust the contentInset wordsTableView.

ContentInset = UIEdgeInsetsMake(0, 0, 0, 10); // Scroll to the current text field wordsTableView scrollToRowAtIndexPath:NSIndexPath indexPathForRow:z inSection:0 atScrollPosition:UITableViewScrollPositionBottom animated:YES; } } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { // Determine which row is being edited int z = textField. Tag; if (z > 4) { // resize the UITableView to the original size self.wordsTableView. Frame = CGRectMake(0.0,44.0,320.0,416.0); // Undo the contentInset wordsTableView.

ContentInset = UIEdgeInsetsMake(0, 0, 0, 0); } return YES; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { // Dismisses the keyboard when the "Done" button is clicked textField resignFirstResponder; return YES; }.

You need to resize the tableView itself so that it does not go under the keyboard. -(void) textFieldDidBeginEditing:(UITextField *)textField { // SUPPOSEDLY Scroll to the current text field self.worldsTableView. Frame = CGRectMake(//make the tableView smaller; to only be in the area above the keyboard); CGRect textFieldRect = textField frame; self.

WordsTableView scrollRectToVisible:textFieldRect animated:YES; } Alternatively, you can use a keyboard notification; this works slightly better because you have more information, and is more consistent in terms of knowing when the keyboard is coming up: //ViewDidLoad NSNotificationCenter defaultCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil; NSNotificationCenter defaultCenter addObserver:self selector:@selector(keyboardWillde:) name:UIKeyboardWilldeNotification object:nil; And then implement: - (void)keyboardWillShow:(NSNotification *)notification { } - (void)keyboardWillde:(NSNotification *)notification { }.

I resized the table to 200 pixels to fit above the keyboard, but the following will only scroll the table UP to the first row, not down to a hidden row: CGRect textFieldRect = textField frame; self. WordsTableView scrollRectToVisible:textFieldRect animated:YES; – Lauren Quantrell Mar 10 at 21:51 Can you use scrollToRowAtIndexPath: instead? That would be simpler.

If not, the issue is that textFieldRect is the frame of the textField, which its frame in relation to its superview (which is the contentView, or tableViewCell). You need to convert the rect to the coordinate system of the tableView, instead of the tableViewCell. Use convertRect:toView: for that.

– GendoIkari Mar 10 at 22:08 I figured that textFieldRect is giving me just the first row, since it's a frame inside a view, as you suggest, which is why it scrolls only to the topmost row. I have no idea how to use convertRect:toView: – Lauren Quantrell Mar 10 at 22:29 CGRect myFrame = textField convertRect:textFieldRect toView:self. WorldsTableView; Then scroll to myFrame instead of to textFieldRect.

– GendoIkari Mar 10 at 22:47 CGRect myFrame = textField convertRect:textFieldRect toView:self. WorldsTableView; is not working either... – Lauren Quantrell Mar 107 at 1:48.

I have a UITextField in a table view on a UIViewController (not a UITableViewController). If the table view is on a UITableViewController, the table will automatically scroll to the textField being edited to prevent it from being hidden by the keyboard. But on a UIViewController it does not.

I have tried for a couple of days reading through multiple ways to try to accomplish this and I cannot get it to work. However this only scrolls the table to the topmost row. What seems like an easy task has been a couple of days of frustration.

I suspect I can get the tableView to scroll properly if I can somehow pass the indexPath. Row in the textFieldDidBeginEditing method? Any help is appreciated.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions