Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems 'Resizing' UITextView when the Keyboard is Shown, in iOS 7

I'm targeting iOS7 only.

I want to 'resize' a UITextView when the keyboard is shown, so that all of the text can be seen, rather than being hidden behind the keyboard.

I've considered a few different approaches to this...

1) Change the frame of the UITextView when the keyboard shows.

The following question details the same problem that I have with this approach - despite the frame being set correctly, the last line/cursor will extend beyond the bounds of the UITextView, and therefore be out of sight:

UITextView cursor below frame when changing frame

You can see this effect from the following screen shot. The UITextView has a green background. It's been added to a UIView with a red background. The arrow shows where the cursor is...

enter image description here

2) Changing the contentInset property on the UITextView

I believe that this recommended/preferred approach. Note, I've read the Apple documentation for resizing views based on the keyboard appearing/disappearing:

https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7

With my code, I'm getting no effect when I change the bottom component of the UIEdgeInsets.

Same example as above, green UITextView on a red UIView, the text disappears underneath the keyboard:

enter image description here

And here's the code:

- (void)keyboardWillShow:(NSNotification*)notification
{
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets insets = _textView.contentInset;
    insets.bottom += keyboardSize.height;
    _textView.contentInset = insets;
    _textView.scrollIndicatorInsets = insets;
}

Note: the scrollIndicatorInsets part works fine. It's hard to depict with a screen shot, but the scroll indicator starts and stops at the right place and appears to be the correct size.

I've read a bunch of questions where people have had a similar problem.

3) Changing the textContainerInset on the UITextView

The answer on this question suggests using textContainerInset instead of contentInset on iOS 7:

UITextView contentInset not working in UITextView on iOS 7?

I've tried this also, but still don't manage to resize the UITextView.

In this question, 'mann' is also having problems with both the contentInset and the textContainerInset:

UITextView content Inset Bottom not working iOS7

Questions

  • which is the correct/preferred approach, textContainerInset or contentInset?
  • in the code shown above for setting the contentInset, am I missing something? Is there something else I need to set?
  • are these bugs with iOS 7?

Thanks

like image 608
Gavin Hope Avatar asked Dec 15 '13 18:12

Gavin Hope


1 Answers

It's a bug in iOS 7. Pete Stenberger solved this with a subclass of UITextView. You can find it here:

https://github.com/steipete/PSPDFTextView

Here's more info:

http://petersteinberger.com/blog/2014/fixing-uitextview-on-ios-7/

like image 159
José Manuel Sánchez Avatar answered Sep 28 '22 04:09

José Manuel Sánchez