Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 792
CoderOgden Avatar asked Apr 19 '26 16:04

CoderOgden


1 Answers

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.

like image 161
emrcftci Avatar answered Apr 23 '26 07:04

emrcftci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!