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.
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.
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.
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:
First thing to do, add the scrollview and add the constraint for leading, trailing, top and bottom with respect to superview.
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.
Constraints for content view
Bottom constraint set to zero low priority.
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:
Note that the bottom constraint in this case is required priority.
Final output:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With