eloew
eloew

Reputation: 47

iOS UIScrollView Issues

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

Answers (3)

Samiul Islam Sami
Samiul Islam Sami

Reputation: 850

@eloew You are almost done. You just need to adjust few things.

  1. Your view width have to equal with scrollView width.
  2. Put the height and bottom constant of Second textField/Label.
  3. Done

Upvotes: 1

Matic Oblak
Matic Oblak

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:

  • View width equals to superview width
  • First label leading and top are pinned to superview leading and top
  • First text field leading to First label trailing
  • First text field trailing to Superview trailing
  • First text field center vertically to First label
  • Outlet label leading to Superview
  • Outlet label trailing to Superview
  • Outlet label top to First label bottom
  • Second label leading to Superview leading
  • Second label top to Outlet label bottom
  • Second text field leading to Second label trailing
  • Second text field trailing to Superview trailing
  • Second text field center vertically to Second label
  • Second label bottom to Superview bottom

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

Pancho
Pancho

Reputation: 4143

Short answer is yes it is possible. Simply add a view as a subview in your scroll view and set its constraints.

Apple's documentation

It is simple enough task, you can even find examples here on SO.

Upvotes: 0

Related Questions