0

I'm using Xcode 7 and Swift 2.

I currently have the following structure for my app scene:
Scene -> View -> Stack View

The problem is that I've run out of space vertically, so I want to throw the Stack View into a Scroll View. When I do so however, the content gets pushed off the page horizontally. I have no idea why as without the scroll view everything fits flush within the screen. I haven't added any additional constraints.

Any ideas?

Wain
  • 118,658
  • 15
  • 128
  • 151
Jason
  • 808
  • 6
  • 25

2 Answers2

0

The whole point of a scroll view is that it fits to its content and allows you to scroll to it. So, if you don't constraint the content it'll take whatever size it naturally wants.

So, basically, you need to constrain the width of the content to be the same as the width of the scroll view so that it will grow vertically based on its intrinsic requirements. i.e. set the width of the scroll view and the stack view to be equal.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I added a constraint so that Stack View.width = width, it looks the same. I also set the content hugging priority to be 1000 horizontally but it's still pushed off-page. I know I should probably provide more info but I'm not sure what would help – Jason Jul 26 '16 at 16:58
0

So it turns out that you need a content view (which is a regular uiview) first, so the final structure is

View -> Scroll View -> Content View -> Stack View

Now here's the critical part: You need to set an equal width constraint with the Content View to the View, not the Scroll View. The scroll view doesn't care about the size of your layout as you see it in Xcode, so you need to constrain everything inside of the scroll view to something outside of the scroll view. The stack view's constraints are four 0s to the content view (I could probably just have the stack view do the content view's job, but it works so I'm not gonna mess with it for now)

Step 5 of this link provided by Nick above gave me the idea

Community
  • 1
  • 1
Jason
  • 808
  • 6
  • 25