Created
November 8, 2012 12:17
-
-
Save calimarkus/4038466 to your computer and use it in GitHub Desktop.
Update UITableView insets on keyboard changes
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)registerForNotifications | |
{ | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(keyboardStateChanged:) | |
name:UIKeyboardDidShowNotification | |
object:nil]; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(keyboardStateChanged:) | |
name:UIKeyboardWillHideNotification | |
object:nil]; | |
} | |
- (void)dealloc | |
{ | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
- (void)keyboardStateChanged:(NSNotification*)notification; | |
{ | |
CGRect keyboardRect = CGRectZero; | |
[[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardRect]; | |
UIEdgeInsets newInsets = UIEdgeInsetsZero; | |
if(notification.name == UIKeyboardDidShowNotification) { | |
// thats only correct, if your view ends at the bottom of the screen | |
// otherwise subtract the difference from the inset | |
newInsets.bottom = keyboardRect.size.height; | |
} | |
[UIView animateWithDuration:0.5 animations:^{ | |
self.tableView.contentInset = newInsets; | |
self.tableView.scrollIndicatorInsets = newInsets; | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment