3

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?

steveSarsawa
  • 1,559
  • 2
  • 14
  • 31
Casper Lindberg
  • 600
  • 1
  • 6
  • 21

1 Answers1

11

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.

emrcftci
  • 3,355
  • 3
  • 21
  • 35
  • Hi Emre, thanks for your answer! My ScrollView constraints are as you listed. In my ContentView, I had forgot equalHeight. So I set it to equal height (to the top view of the hierarchy). Then I got a warning, that the collectionView need constraints for: Y position of height. So I added height constraint for the collectionView and it didn't work. Then I added height constraint for the "inner stack view" and that didn't work neither. Am I missing something out, do you need to see more code/constraints? Thanks in advance, Casper – Casper Lindberg Jul 22 '19 at 08:56
  • 1
    If the scrollview has to be scrolled vertically then ContentView's height should not be set. It should be derived from the data inside. (ie, each view should put down its height and system will calculate the final height.). Setting a height will actually create and conflict and then break it based on priority. – Cerlin Jul 22 '19 at 09:02
  • The fun thing is that my code/constraints actually has worked when I had an image view instead of a collection view. But when I added the collection view it didn't work... – Casper Lindberg Jul 22 '19 at 09:05
  • 1
    @CoderOgden This means the auto layout is not able to calculate the UICollectionViews size. This is the reason i asked to set a height and width constraint for now and check if that scrolls. Set the height and width to a large number so that the view overflows – Cerlin Jul 22 '19 at 09:07
  • I get it! Now it works. I am not constraining height of the stack view but setting height of my collectionView. Thanks both of you! – Casper Lindberg Jul 22 '19 at 09:30