Created
October 9, 2013 04:29
-
-
Save jonfriskics/6896230 to your computer and use it in GitHub Desktop.
UITextView and the keyboard
This file contains 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
[[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