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?

4
  • 1
    Try fixing a dummy height/width for Image Collection View using constraints to see if thats the issue. Commented Jul 22, 2019 at 8:33
  • Hi Berlin Boss! I tried it and some weird behavior appeared. The view was like flickering but it didn't scroll. Commented Jul 22, 2019 at 8:59
  • If possible, create a new project, move this design into that, bundle it, upload and share us the link. Commented Jul 22, 2019 at 9:06
  • Good idea! Thanks, will hopefully not get back with more trouble ;) Commented Jul 22, 2019 at 9:08

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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
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.
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...
@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
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.