Created
March 8, 2017 18:39
-
-
Save bocato/54f7848ba555ecca0699fc549607058c to your computer and use it in GitHub Desktop.
Hide show keyboard on TableView/CollectionView
This file contains 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
#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