Skip to content

Instantly share code, notes, and snippets.

@calimarkus
Created November 8, 2012 12:17
Show Gist options
  • Save calimarkus/4038466 to your computer and use it in GitHub Desktop.
Save calimarkus/4038466 to your computer and use it in GitHub Desktop.
Update UITableView insets on keyboard changes
- (void)registerForNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardStateChanged:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardStateChanged:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardStateChanged:(NSNotification*)notification;
{
CGRect keyboardRect = CGRectZero;
[[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardRect];
UIEdgeInsets newInsets = UIEdgeInsetsZero;
if(notification.name == UIKeyboardDidShowNotification) {
// thats only correct, if your view ends at the bottom of the screen
// otherwise subtract the difference from the inset
newInsets.bottom = keyboardRect.size.height;
}
[UIView animateWithDuration:0.5 animations:^{
self.tableView.contentInset = newInsets;
self.tableView.scrollIndicatorInsets = newInsets;
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment