Last active
December 28, 2015 19:19
-
-
Save cwagdev/7549537 to your computer and use it in GitHub Desktop.
Handle keyboard presentation and dismissal with scroll views
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)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