0

I need to create the following layout:

enter image description here

It's a UIScrollView with a label and a UIStackView inside. I want the UIStackview to be at a minimum distance of the blue label of 20 and a minimum of 40 from the bottom...

I have done the following but of course it's not behaving as I want. I can't figure it out!

NSLayoutConstraint.activate([
      pinkScrollView.topAnchor.constraint(equalTo: view.topAnchor),
      pinkScrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
      pinkScrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
      pinkScrollView.widthAnchor.constraint(equalTo: view.widthAnchor),
      pinkScrollView.bottomAnchor.constraint(equalTo: orangeLabel.topAnchor),

      blueLabel.topAnchor.constraint(equalTo: pinkScrollView.topAnchor),
      blueLabel.leadingAnchor.constraint(equalTo: pinkScrollView.leadingAnchor),
      blueLabel.trailingAnchor.constraint(equalTo: pinkScrollView.trailingAnchor),
      blueLabel.widthAnchor.constraint(equalTo: view.widthAnchor),

      greenStackView.topAnchor.constraint(greaterThanOrEqualTo: blueLabel.bottomAnchor, constant: 20),
      greenStackView.leadingAnchor.constraint(equalTo: pinkScrollView.leadingAnchor),
      greenStackView.trailingAnchor.constraint(equalTo: pinkScrollView.trailingAnchor),
      greenStackView.widthAnchor.constraint(equalTo: view.widthAnchor),
      greenStackView.bottomAnchor.constraint(lessThanOrEqualTo: pinkScrollView.bottomAnchor, constant: -40),

      orangeLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor),
      orangeLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor),
      orangeLabel.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
      orangeLabel.heightAnchor.constraint(equalToConstant: 40)
])
mfaani
  • 33,269
  • 19
  • 164
  • 293
Martin Perez
  • 147
  • 1
  • 8
  • `pinkScrollView.widthAnchor.constraint(equalTo: view.widthAnchor)` isn't needed. Can you show the 1. setup you have for your stackview 2. what's subviews it has 3. The constraints associated with items inside it? Basically your stackview needs to have constraints that push the scrollview to enlarge _vertically_. otherwise the scrollview's height would be ambiguous. Did you check the 'view debug hierarchy' to see [purple](https://stackoverflow.com/questions/49664266/xcode-debug-view-hierarchy) runtime errors? – mfaani Sep 11 '20 at 20:46
  • For a fundamental understanding see [here](https://stackoverflow.com/questions/19036228/uiscrollview-scrollable-content-size-ambiguity/63789184#63789184). You also might wanna just dump the stackview inside a viewcontroller and see if it sizes as you expect, then throw it inside a scrollview. – mfaani Sep 11 '20 at 20:46
  • The description of your layout is confusing... The Pink rect is the frame of your scroll view? And you want you Green stack view, which will hold an image view (or additional views?), centered in a space that is 20-pts below the bottom of the variable multi-line label, and 40-pts above the bottom of the scroll view frame? What's going to be scrolling? Do you mean that if you have a lot of text at the top, and a large image view (or several views) in the Green stack view, you want to be able to scroll up so there is 40-pts of empty space at the bottom? – DonMag Sep 13 '20 at 14:59

0 Answers0