Search code examples
iosswiftuikituitextviewuipopover

UITextView textContainerInsets changing with preferredContentSize


At the top of a popover I've got a UITextView with a height of 100px whose text container insets I've adjusted using textView.textContainerInset = UIEdgeInsets(...). The popover's height changes over time using self.navigationController?.preferredContentSize.height = someHeight where self is a subclass of UIViewController and UIPopoverPresentationControllerDelegate.

When I use that method to adjust the height of the popover, the text view's text container drops about 50px as if it's insets are animating. I've tried explicitly resetting the text container insets after adjusting the height to no avail.

Opening the view debugger shows that the text view itself is still 100px tall but clearly shows the text container (which is listed as a _UITextContainerView and isn't available to access directly) has dropped about 50px.

Any idea what's causing the insets or the _UITextContainerView to drop like that?


Solution

  • I ended up setting self.automaticallyAdjustsScrollViewInsets = false on the view controller and that did the trick.

    I suspect this worked because a UITextView has a scrolling behavior when the text exceeds its bounds (not sure if it's a full fledged scroll view inside or not, I've read in some places that it is and others say it isn't.) And because the height of the preferred content size of the containing view controller had changed, it had an effect on the text view's content (this content being the previously mentioned _UITextContainerView.) So by setting self.automaticallyAdjustsScrollViewInsets = false, this stopped the text view from dropping its text container by about 50px which ended up being 44px (the delta between the old and new height. Go figure!)