Reputation: 47
I am unable to get a scrollview to scroll if I use a view in the scroll view. I followed the example https://useyourloaf.com/blog/scroll-view-layouts-with-interface-builder/ , which used a stacked view in a scroll view. When I follow this example and use a stack view, it works. My UI requires a view because of the layout I need for the controls.
I have a sample project at: https://github.com/eloew/ScrollViewTest which illustrates the problem. I have used the storyboard so no code to post here.
Is it possible to use a view in a scrollview?
Upvotes: 1
Views: 62
Reputation: 850
@eloew You are almost done. You just need to adjust few things.
Upvotes: 1
Reputation: 16774
The procedure you are using is correct but it is sometimes tricky to setup constraints. Your view needs to have either implicit or explicit width and height. Then it needs to be inserted into scroll view and have all 4 border constraints setup.
Looking at your project you have missed a few constraints. To debug a view like that it is easiest to first set it up outside the scroll view. Take your view outside it and setup leading and top constraints to it's superview. Now modify your constraints until you see a desired result.
For your specific case I used:
After all these are set I have a nicely layout view without a scroll view. There should be no errors visible.
Now add a scroll view. Pin it to leading, trailing, top and bottom. Then drag your view inside your scroll view and this view to scrollview leading, trailing, top and bottom and set equal width between your view and your scrollview. That should be it.
Upvotes: 1
Reputation: 4143
Short answer is yes it is possible. Simply add a view as a subview in your scroll view and set its constraints.
It is simple enough task, you can even find examples here on SO.
Upvotes: 0