Egor
Egor

Reputation: 485

UITextView stretches horizontally but not vertically inside UIScrollView

My task is very simple - just display vertically scrolling text inside custom UIView. I have an UIScrollView with some container with one UITextView in it. Container is needed to add more items later. I use IB(xib file) to add autolayout constraints like shown below:

Screenshot

But everything I see is:

Simulator

And it's scrolling horizontally :-(

I've tried setting contentSize of UIScrollView:

Thanks a lot!

Edit: Text View's and Container View's intristic size are set to "placeholder". I understand that I should limit width of ContainerView, but it should work with all screen sizes and orientations, so setting width in code, IMO, worse than setting constraints.

Upvotes: 0

Views: 445

Answers (1)

Egor
Egor

Reputation: 485

Yeah! I've found the solution!

If someone facing the same problem:

You should add left and right constraints not to UIScrollView, but to superview of UIScrollView (to super-super-view).

Unfortunately, this cannot be done in IB, but you can do it within code:

[self addConstraint:[NSLayoutConstraint constraintWithItem:self.view 
    attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual
    toItem:self.containerView attribute:NSLayoutAttributeLeft
    multiplier:1.0 constant:-30]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.view
    attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual
    toItem:self.containerView attribute:NSLayoutAttributeRight
    multiplier:1.0 constant:30]];

Where self.containerView is view inside UIScrolllView and self.view is view outside UIScrollView

So:

You set vertical constraints to UIScrollView and horizontal constraints to top-level view.

Upvotes: 1

Related Questions