Skip to content

Instantly share code, notes, and snippets.

@cwagdev
Last active December 28, 2015 19:19
Show Gist options
  • Save cwagdev/7549537 to your computer and use it in GitHub Desktop.
Save cwagdev/7549537 to your computer and use it in GitHub Desktop.
Handle keyboard presentation and dismissal with scroll views
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)handleKeyboardShowNotification:(NSNotification *)note {
CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, keyboardFrame.size.height, 0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
}
- (void)handleKeyboardHideNotification:(NSNotification *)note {
NSTimeInterval animationDuration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationOptions animationCurve = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue] << 16;
[UIView animateWithDuration:animationDuration delay:0 options:animationCurve animations:^{
self.tableView.contentInset = UIEdgeInsetsZero;
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
} completion:nil];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment