Search code examples
swiftuiscrollviewuistackview

Swift - StackView in ScrollView doesn't scroll


I am trying to make a scrollable stack view inside a scrollview but it doesn't work.

I have built a structure like this: Structure Image

If you can't see the image, this is the structure:
UIScrollView constrained to superview in all 4 directions
UIStackView (constrained to scrollview in all 4 directions + equal width)
Image Collection View (not constrained)
UIStackView (not constrained)
Stack of Labels (not constrained)

It doesn't scroll, can anyone see what I am missing?


Solution

  • You should set ContentView in your ScrollView with constraints like:

    ScrollView's Constraints:

    • Leading to superView
    • Trailing to superView
    • Top to superView
    • Bottom to superView

      These constraint's constants are 0

    ContentView's Constraints:

    • Leading to superView
    • Trailing to superView
    • Top to superView
    • Bottom to superView

    • Equal height to ViewController's View (which is in the top of the view hierarchy)

    • Equal width to ViewController's View 

      Note: You should set ContentView's height constraint's priority to 700 etc. (lower than default high value)

    ScrollView and ContentView's view hierarchy and constraints

    Attention:

    Your stackViews and collectionView must have height for scrollable.

    I hope it is works.

    Enjoy.