0

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!

Wain
  • 118,658
  • 15
  • 128
  • 151
Tarun
  • 23
  • 1
  • 7
  • see this answer http://stackoverflow.com/questions/25782352/what-is-causing-this-unwanted-content-inset-with-uitextview-in-ios-8-not-there – Deepesh Nov 25 '14 at 07:15
  • The link didn't help. I am getting extra space at the end of the text. I've already switched automaticallyAdjustScrollViewInsets to NO. – Tarun Nov 25 '14 at 07:47

1 Answers1

2

Solved it. The problem is very rare but if someone is stuck, here's the solution.

I used to set the text of the textView in viewDidLoad like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.textView setScrollEnabled:YES];
    [self.textView setText:#-some text-#];
    [self.textView setScrollEnabled:NO];
}

I switched off scroll so that the text view doesn't scroll to the bottom on setting text. And that created the problem of extra scrolling. (I'm not sure why)

I used this solution to solve the problem: https://stackoverflow.com/a/3287419

Community
  • 1
  • 1
Tarun
  • 23
  • 1
  • 7