Reputation: 1532
I have a static tableView
with one textfield
in each cell
. The cell height is 50. I have the following method to deal with keyboard:
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
}
Now, when I enter textField
, if it is located under the keyboard, it automatically scrolls to the textField
. Actually, I'm not sure how this is happening, since I'm not configuring this behavior anywhere. What is going on here?
And what I actually want is to scroll to the bottom of the cell
, instead of textField
. At the moment, it scrolls to the textField
but it cuts off part of the cell
and it doesn't look good.
The contentInset.bottom
is right, since I can manually scroll to the last cell perfectly when keyboard is present.
I'm probably missing some native behavior.
Upvotes: 1
Views: 89
Reputation: 62686
I'd do it something like this (not tested). Figure out the cell's frame in the main view's coordinate system. If the bottom of the cell below the top of the keyboard, adjust the table view's content offset by the difference...
UITableViewCell *cell = // get the cell containing the text field
CGRect bounds = [self.tableView convertRect:cell.frame toView:self.view];
CGFloat cellBottom = bounds.origin.y + bounds.size.height;
if (cellBottom > kbSize.height) {
[self.tableView setContentOffset:(cellBottom-kbSize.height) animated:YES];
}
There are several SO answers that can help you get the cell from the text field. The best way is to walk up the superview pointers till you find a UITableViewCell. Here's an example where I answered this for a collection view
Upvotes: 1
Reputation: 4425
As @rebello95 said, this is the expected behavior in a UITableViewController. To avoid this, just inherit your view controller from UIViewController instead of UITableViewController and add the table view to the view by yourself.
Upvotes: 0