ios7uitextviewios8nslayoutmanagernstextcontainer

What is Causing this Unwanted Content Inset with UITextView in iOS 8 (not there in iOS 7)?


I've been creating UITextViews programatically, using auto layout, in iOS 7. Something like this:

_textView = [UITextView new];
_textView.translatesAutoresizingMaskIntoConstraints = NO;
_textView.font = [UIFont systemFontOfSize:18.0];
_textView.delegate = self;
[self.view addSubview:_textView];

In addition to the other constraints I've created, I have this one for the bottom of the text view:

_textViewBottomConstraint = 
       [NSLayoutConstraint constraintWithItem:_textView
                                                     attribute:NSLayoutAttributeBottom
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.bottomLayoutGuide
                                                     attribute:NSLayoutAttributeTop
                                                    multiplier:1.0
                                                      constant:0.0];

[self.view addConstraint:_textViewBottomConstraint];

So that I can change the constraint when the keyboard shows:

- (void)keyboardDidShow:(NSNotification*)sender
{
    CGRect keyboardFrame = 
                        [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect convertedFrame = 
                        [self.view convertRect:keyboardFrame fromView:self.view.window];
    _textViewBottomConstraint.constant = -convertedFrame.size.height;
    [_textView layoutIfNeeded];
}

That's all been working fine...

Problem

I've just switched over to Xcode 6 and iOS 8 (late, I know) - and the text within the UITextView now has an inset at the top. E.g. (Text view background is yellow):

enter image description here

What's causing the inset? Is it connected to NSTextContainer and textContainerInset?

If yes, I'd appreciate some guidance for programatically creating a UITextView along with the necessary NSTextContainer and NSLayoutManager; then setting textContainerInset. (I'll be using auto layout, programatically).

Thanks.


Solution

  • I think automaticallyAdjustsScrollViewInsets works a bit differently in iOS 8. In 7, it only applied if the controller's view property was a scrollView. In 8, it seems to apply to scroll views that are subviews of the controllers' view.