Skip to content

Instantly share code, notes, and snippets.

@bryanluby
Created February 19, 2014 03:51
Show Gist options
  • Save bryanluby/9085761 to your computer and use it in GitHub Desktop.
Save bryanluby/9085761 to your computer and use it in GitHub Desktop.
Handle UITextView adjustments when showing and hiding the keyboard.
- (void)handleKeyboardShow:(NSNotification *)notification
{
NSDictionary *dict = notification.userInfo;
NSValue *rectValue = dict[UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [rectValue CGRectValue];
UITextView *activeTextView;
if ([self.textView1 isFirstResponder]) {
activeTextView = self.textView1;
} else if ([self.textView2 isFirstResponder]) {
activeTextView = self.textView2;
}
if (CGRectIntersectsRect(keyboardRect, activeTextView.frame)) {
CGFloat textViewBottom = CGRectGetMaxY(activeTextView.frame);
CGFloat keyboardTop = CGRectGetMinY(keyboardRect);
CGFloat heightOffset = textViewBottom - keyboardTop;
[self.scrollView setContentOffset:CGPointMake(0, heightOffset) animated:YES];
}
}
- (void)handleKeyboardHide:(NSNotification *)notification
{
[self.scrollView setContentOffset:CGPointZero animated:YES];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment