Skip to content

Instantly share code, notes, and snippets.

@ahmattox
Last active December 19, 2015 01:08
Show Gist options
  • Save ahmattox/5873156 to your computer and use it in GitHub Desktop.
Save ahmattox/5873156 to your computer and use it in GitHub Desktop.
Resize a UIScrollview when the keyboard is shown based on the overlap between the scroll view and keyboard frame
- (void) keyboardWillChangeFrame:(NSNotification *)notification {
CGRect originalKeyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect keyboardFrame = [[[UIApplication sharedApplication] keyWindow] convertRect:originalKeyboardFrame toView:self];
CGFloat keyboardHeight = CGRectIntersection(keyboardFrame, self.contentScrollView.frame).size.height;
[UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] delay:0 options:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{
self.contentScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, keyboardHeight, 0);
self.contentScrollView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0);
} completion:nil];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment