Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolayout and Scrollview width fixed to screen (not content)

I have followed and number of exmaples (most of which are the same) such as https://www.youtube.com/watch?v=UnQsFlMGDsI

I opena new project, drop a scrollview in and then a "content" view in that, constrain the scrollview to it's superview (top, bottom, left and right) then the Content view is constrained to the inside of the scrollview and then it's width and hight to the scrollview superview. Scrollview in red and content view black.

I drop a Textfield in and then constraint it to the top, leading and trailing of the content view. It all looks good in storyboard but when I run it the content view is not there and the textfield is the width of the objects in it (which are based on the textfield) in it.

None fixed height of content view

I then change the content view hight to be a fixed 800 and I can now see the content view but width is fixed by the size of the textfield (not the other way around), i.e. if I add more "Test info" text it all gets wider.

enter image description here

This is not my real project but I just wanted to eliminate reasons so did this from scratch and it does the same. I can not see what I am missing (hoping a tick box).

If I force the textfield to be 700 wide it scrolls left horizontally, I can not see how to pin the content view width to the screen size.

like image 940
Recycled Steel Avatar asked Dec 18 '22 22:12

Recycled Steel


1 Answers

UPDATED ANSWER

Sorry about the answer that I previously posted. I missed some points in that. Here I will explain to you step by step what need to be done(refer screenshots).

Here is an overview of the entire view with constraints added:

enter image description here

First thing to do, add the scrollview and add the constraint for leading, trailing, top and bottom with respect to superview.

enter image description here

Next thing to do, add a UIView inside the scroll view. Now this part is tricky, the view you just added need to act as the content view of the scroll view. So whatever views you need to add to the scroll view should actually go inside this view. For this view, set the constraints like, top, bottom, leading and trailing. But the tricky part here is, set the bottom constraint to superview(ie, the scrollview) as zero with low priority(250). Also set the width of this view as equal to the scroll view's superview, that is the ViewController's view as well as the scroll view.

enter image description here

Constraints for content view

enter image description here

Bottom constraint set to zero low priority.

enter image description here

Equal width with reference to scrollview's superview as well as the scroll view.

The bottom constraint is set to low priority since the constraints of the inner views need to be given higher priority when it comes to bottom constraint of the view for the sake of scrolling to work.

Now add the required view inside the content view, in your case the text field. Add all the required constraints with respect to the content view(top, leading, trailing, bottom). Add any number of subviews you wish to add, but ensure you specify the bottom constraint for the bottom most view with respect to the content view, that too with required priority. Here are the constraints for the textfield that I added inside the content view:

enter image description here

Note that the bottom constraint in this case is required priority.

Final output:

enter image description here

Yellow = ViewController's view, Green = scroll view, Red = container view, pink = text field.

A note for anyone who need to make a horizontal scrollview, just switch the width of the content view with height and its bottom constraint with trailing constraint. That is, set equal heights with superview and scrollview and pin trailing space to scroll view with low priority.

like image 95
Harikrishnan Avatar answered Feb 20 '23 15:02

Harikrishnan