Search code examples
iosuiscrollviewautolayoutuistackview

UIStoryBoard Auto Layout embed UIStackView inside UIScrollView


I'm having a problem design my ViewController which contain a some Label and a ImageView. I have embed them in a StackView. My objective is to place a StackView inside a ScrollView so that my content will be scrollable if the content is longer then the screen height

So basically what I have done is

1 - Place a UIScrollView in a scene. Pin the scroll view to its superview without margins.

2 - Place a UIStackView inside the scroll view. Pin all sides of the stack view to the scroll view without margins (yes this is counterintuitive).

3 - Vertical scrolling: Add an equal widths constraint between the scroll view and the stack view

Here is my setting in storyboard

enter image description here

But I'm getting this red warning constrains. Anyone know how to fix this? Maythanks

enter image description here


Solution

  • Try example given at this Link It is using following steps

    1. Create a UIScrollView, and set its constraints.
    2. Add a UIStackView to the UIScrollView
    3. Set the constraints: Leading, Trailing, Top & Bottom should be equal to the ones from UIScrollView
    4. Set up an equal Width constraint between the UIStackView and UIScrollView.
    5. Set Axis = Vertical, Alignment = Fill, Distribution = Equal Spacing, and Spacing = 0 on the UIStackView

      https://github.com/ar-juan/uistackview-in-uiscrollview