Skip to content

Instantly share code, notes, and snippets.

@kenn
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenn/11166009 to your computer and use it in GitHub Desktop.
Save kenn/11166009 to your computer and use it in GitHub Desktop.
- (void)keyboardWillHideOrShow:(NSNotification *)notification
{
UIEdgeInsets tableInset = self.tableView.contentInset;
CGPoint scrollOffset = self.tableView.contentOffset;
UIToolbar *toolbar = self.navigationController.toolbar;
CGRect toolbarFrame = toolbar.frame;
NSDictionary *userInfo = notification.userInfo;
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGFloat keyboardHeight = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
if ([notification.name isEqualToString:@"UIKeyboardWillShowNotification"]) {
toolbarFrame.origin.y -= keyboardHeight;
tableInset.bottom += keyboardHeight;
scrollOffset.y += keyboardHeight;
} else {
toolbarFrame.origin.y += keyboardHeight;
tableInset.bottom -= keyboardHeight;
scrollOffset.y -= keyboardHeight;
if (scrollOffset.y < -tableInset.top) {
scrollOffset.y = -tableInset.top;
}
}
// Run animation
[UIView beginAnimations:nil context: nil];
[UIView setAnimationCurve:curve];
[UIView setAnimationDuration:duration];
toolbar.frame = toolbarFrame;
self.tableView.contentInset = tableInset;
self.tableView.contentOffset = scrollOffset;
[UIView commitAnimations];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment