Skip to content

Instantly share code, notes, and snippets.

@james-b-kelly
Created February 16, 2015 07:37
Show Gist options
  • Save james-b-kelly/4bfc06eb6a52e1f48815 to your computer and use it in GitHub Desktop.
Save james-b-kelly/4bfc06eb6a52e1f48815 to your computer and use it in GitHub Desktop.
Adjust table/collection view height for keyboard.
//Set up your tableview/collection view in IB with a bottom layout constraint with an IBOutlet.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(adjustTableViewHeight:)
name:UIKeyboardDidShowNotification
object:nil];
}
- (void)adjustTableViewHeight:(NSNotification*)notification {
CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGPoint keyboardOriginInView = [self.view.window convertPoint:keyboardFrame.origin toView:self.view];
self.tableViewBottomConstraint.constant = CGRectGetHeight(self.view.frame) - keyboardOriginInView.y;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment