1

No matter which settings I try to change, resignFirstResponder always forces the text to scroll to the top of a UITextView

I would like the keyboard to disappear but the content offset to remain the same.

Any solutions would be greatly appreciated.

Nate4436271
  • 860
  • 1
  • 10
  • 21

2 Answers2

3

This is caused by a UIScrollView bug in iOS 8. Hopefully in iOS 9 they fix it. I only assume that its a bug because resignFirstResponder didn't have this behavior in iOS 7. Maybe its a feature.

The only way I found to prevent resignFirstResponder and setText from resetting the contentOffset was the following:

Swift:

textView.layoutManager.allowsNonContiguousLayout = false

Objective C

textView.layoutManager.allowsNonContiguousLayout = NO;

Another reason I think its a bug is because the documentation says that it is set to NO by default, but when you print out the variable without setting it, its true.

More info on NSLayoutManager can be found here

Unome
  • 6,750
  • 7
  • 45
  • 87
2

in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

add this function

 - (void)keyboardWillHide:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

[UIView animateWithDuration:animationDuration animations:^{
[_textView scrollRangeToVisible:NSMakeRange([_textView.text length], 0)];
}];
}
Jincy Sam
  • 421
  • 2
  • 11
  • 1
    I did try this. What happened for me is it would jump to the top, then animate down. Not exactly the solution I was looking for. It didnt keep the contentOffset the same, the content offset still gets reset to (0,0), then it scrolls down to the bottom of the page. – Nate4436271 Aug 11 '15 at 17:25