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:
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:
How to create bottom view and stretch ScrollView to fit content.
source to share
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
source to share