Height of UIScrollView when adding content programmatically to UIStackView

I am trying to make a view containing UIScrollView

. This one UIScrollView

contains 3 UIViews

. The latter contains another UIStackView

one that I wanted to fill in at runtime.

Here is a picture of the storyboard:

Storyboard

enter image description here

But when I add content at runtime in the second UIStackView

, the height ScrollView

remains the same.

The second is UIStackView

defined as the first:

  • Axis: vertical
  • Alignment: fill
  • Spread: Equal Distance

Then I use:

mStackView.addArrangedSubview(matProgress)

      

The result is below:

enter image description here

How to create bottom view and stretch ScrollView to fit content.

+3


source to share


2 answers


Layout can be done using automatic layout and constraints.

The key points are:

  • do not set height limits for any StackView
  • set the "Main" StackView Distribution

    toEqual Spacing

  • set basic StackView Leading, Trailing, Top and Bottom constraints on its Superview (this is a ScrollView)
  • you also need to set the width limit on the "Main" StackView to control the horizontal content


The only quirk will be at startup. If you don't have content in the bottom / inner StackView, it will still "exist" in the Main StackView and take up space. There is a trick to get around this, but it must be done in code.

You can see a working example here: https://github.com/DonMag/StackyScrolly

+2


source


If you have UIScrollView

and child views are auto-layout. When programming, the scroll height will stretch the inner views to fit the automak.



So, you code the height of the third view and set the height of the UIScrollView to view1.height + view2.height + expectedThirdViewHeight.

0


source







All Articles