Skip to content

Instantly share code, notes, and snippets.

@jonfriskics
Created October 9, 2013 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonfriskics/6896230 to your computer and use it in GitHub Desktop.
Save jonfriskics/6896230 to your computer and use it in GitHub Desktop.
UITextView and the keyboard
[[NSNotificationCenter defaultCenter] addObserverForName:@"UIKeyboardDidShowNotification" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
CGRect cursorRect = [self.textView caretRectForPosition:self.textView.selectedTextRange.start];
if(CGRectGetMaxY(cursorRect) > CGRectGetHeight(self.view.frame) - self.topLayoutGuide.length - 216) {
[UIView animateWithDuration:1.0f animations:^{
self.textView.contentOffset = CGPointMake(0, 216);
} completion:^(BOOL finished) {
}];
}
}];
[[NSNotificationCenter defaultCenter] addObserverForName:@"UIKeyboardDidHideNotification" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
if(self.textView.contentOffset.x == 0 && self.textView.contentOffset.y == 0) {
// do nothing because we didn't have to offset anything.
} else {
[UIView animateWithDuration:1.0f animations:^{
self.textView.contentOffset = CGPointMake(0, 0);
} completion:^(BOOL finished) {
}];
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment