I have a UITextView covering the whole view controller setup in storyboard. There are constraints to top layout guide, bottom layout guide, leading margin and trailing margin.
I have registered to keyboard notifications to adjust the content insets like this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardAppeared:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDisappeared:)
name:UIKeyboardDidHideNotification
object:nil];
keyboardAppeared's implementation:
- (void)keyboardAppeared:(NSNotification *)notification {
NSDictionary *notificationUserInfo = [notification userInfo];
CGRect keyboardRect = [[notificationUserInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
self.textView.contentInsets = UIEdgeInsetsMake(0, 0, keyboardRect.heignt, 0);
self.textView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, keyboardRect.height, 0);
}
keyboardDisappeared's implementation:
- (void)keyboardDisappeared:(NSNotification *) {
self.textView.contentInsets = UIEdgeInsetsZero;
self.textView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
The trouble here is when the keyboard appears there is an unwanted scroll in the textView if the textView text is less. The unwanted scroll doesn't appear when the text size exceeds the textView height.
Please help!