Created
February 19, 2014 03:51
-
-
Save bryanluby/9085761 to your computer and use it in GitHub Desktop.
Handle UITextView adjustments when showing and hiding the keyboard.
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)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