Skip to content

Instantly share code, notes, and snippets.

@bocato
Created March 8, 2017 18:39
Show Gist options
  • Save bocato/54f7848ba555ecca0699fc549607058c to your computer and use it in GitHub Desktop.
Save bocato/54f7848ba555ecca0699fc549607058c to your computer and use it in GitHub Desktop.
Hide show keyboard on TableView/CollectionView
#pragma mark - Hide/Show Keyboard on TableView
// Configure this when loading the view (can be inside viewDidLoad)
self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillShowWithNotification:) name:UIKeyboardWillShowNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillHideWithNotification:) name:UIKeyboardWillHideNotification object:nil];
// Then implement this
- (void)keyboardWillShowWithNotification:(NSNotification *)notification {
CGRect keyboardFrame = [((NSValue *) notification.userInfo[UIKeyboardFrameEndUserInfoKey]) CGRectValue];
CGFloat bottomInset = keyboardFrame.size.height != 0 ? keyboardFrame.size.height : 0;
[UIView animateWithDuration:0.25 animations:^{
self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.contentInset.top, self.tableView.contentInset.left, bottomInset, self.tableView.contentInset.right);
}];
}
- (void)keyboardWillHideWithNotification:(NSNotification *)notification{
[UIView animateWithDuration:0.25 animations:^{
self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.contentInset.top, self.tableView.contentInset.left, 0, self.tableView.contentInset.right);
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment