Skip to content

Instantly share code, notes, and snippets.

@braking
Last active November 14, 2018 18:46
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save braking/5575962 to your computer and use it in GitHub Desktop.
Save braking/5575962 to your computer and use it in GitHub Desktop.
Adjust content insets of a tableview when keyboard opens.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
NSNumber *rate = notification.userInfo[UIKeyboardAnimationDurationUserInfoKey];
UIEdgeInsets contentInsets;
if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height), 0.0);
} else {
contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.width), 0.0);
}
[UIView animateWithDuration:rate.floatValue animations:^{
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
}];
[self.tableView scrollToRowAtIndexPath:self.editingIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSNumber *rate = notification.userInfo[UIKeyboardAnimationDurationUserInfoKey];
[UIView animateWithDuration:rate.floatValue animations:^{
self.tableView.contentInset = UIEdgeInsetsZero;
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
}];
}
@Hartistic
Copy link

what is self.editingIndexPath?

@juan-sanzone-olx
Copy link

self.editingIndexPath? ???

@matthewbordas
Copy link

It wasn't explained, but through trial and error I figured out self.editingIndexPath = last row in the tableview.

@wuf810
Copy link

wuf810 commented Mar 2, 2016

Helpful but more helpful it self.editingIndexPath was explained.

@forgot
Copy link

forgot commented Apr 7, 2016

For those wondering, self.editingIndexPath is the NSIndexPath of the UITableViewCell containing the view that presented the keyboard.

For example, suppose you have a UITableViewCell containing a UITextField, and your viewController is the textField's delegate. You could use the UITextFieldDelegate methods to obtain the index path like this:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    // Call superView twice to account for the contentView. If the text field isn't a direct subview of the
    // contentView, you'll have to work your way up
    if [textField.superView.superView isKindOfClass:[MyCustomTableViewCell class]] {
        self.editingIndexPath = [self.tableView indexPathForCell: textField.superView.superView];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.editingIndexPath = nil;
}

It doesn't have to be a UITextField becoming the firstResponder that triggers the keyboard, this is just one example to help explain the self.editingIndexPath property.

@mlasy
Copy link

mlasy commented Jun 23, 2017

hmm, i had to change keyboardWillShow to the following:

- (void)keyboardWillShow:(NSNotification *)notification
{
	CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
	UIEdgeInsets contentInsets;
	contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height), 0.0);
	self.tableView.contentInset = contentInsets;
	self.tableView.scrollIndicatorInsets = contentInsets;
	[self.tableView scrollToRowAtIndexPath: editingIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

else the inset was to large in landscape mode.

@mlasy
Copy link

mlasy commented Jun 23, 2017

concerning editIndexPath:
i added

@interface MyTableViewController ()
{
	NSIndexPath* editingIndexPath;
}
@end

to the top of my viewcontroller.

Then i set the indexPath when i call my textedit method.

@bryanaamot
Copy link

bryanaamot commented Oct 29, 2017

Should be UIKeyboardFrameEndUserInfoKey. In iOS11, the height is 0 when you use UIKeyboardFrameBeginUserInfoKey! Also, on iPhoneX the height is 75 pts more in portrait mode. So, you are better off adjusting a constraint of the tableview that is pinned to the bottom of the superview because the keyboard is pinned to the bottom of the superview too. Then you don't need to change the tableview contentInset at all. This works on all devices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment