I've been using the following code in my view controller to update the content offset of a UITextView when the keyboard gets displayed:
- (void)keyboardWasShown:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
UIEdgeInsets contentInsets = UIEdgeInsetsMake( 0.0, 0.0, keyboardRect.size.height, 0.0 );
self.textView.contentInset = contentInsets;
self.textView.scrollIndicatorInsets = contentInsets;
}
With the keyboard showing, manually scrolling the content of the UITextView to the bottom has it properly ending just above the top of the keyboard. -[UITexView scrollRangeToVisible:], however, doesn't seem to take into account the presence of the keyboard any more.
- In iOS 6, the text view scrolled until the specified range was displayed just above the keyboard.
- In iOS 7, the visibility appears to now be based on the frame of the text view and not the content inset, as it used to. So the view will only scroll when the range extends below the frame, and then it will scroll only enough to get that range visible at the bottom of the
Visually, here's what's happening. I built an inline search for my text view with controls to jump between the results (similar to searching in Safari). So in the text view shown here with search results as the user tapped the "next" button, the cyan selection would cycle down through the results. When the user went to the seventh result, the view would scroll until it was visible.
With the keyboard (from the UISearchBar) up on the same search results when the user went to the fifth search result, it would scroll to be just above the keyboard. But only in iOS 6. In iOS 7 no scrolling happens until going to the seventh search result like in the non-keyboard situation, and even then it scrolls the same amount so it's just visible below the bottom of the text view's frame.
Is this a known change in iOS 7? I'm using auto-layout so the next thing I'm going to try is to adjust the text view's bottom spacing constraint to shrink the entire view to avoid the problem, but want to check if there's way to still use my existing code under iOS 7.