Last active
August 29, 2015 14:00
-
-
Save kenn/11166009 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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