swiftautolayoutuistackviewintrinsic-content-size

UIStackView distribution with intrinsic content size of one subview prioritised


  stackView
+------------+                +
|  topView   | height: 50     |
+------------+                |
|            |                |
|            |                | total height: 300
| bottomView | height: 250    |
|            |                |
|            |                |
+------------+                +

I would like to layout two views (topView and bottomView) in a UIStackView (stackView) with these conditions:

None of the distribution modes of UIStackView can cover this.

Setting a constraint with a constant wouldn’t help here, as the intrinsic content height of topView might change.

So I need some Auto Layout features here. I tried setting different content compression resistance priorities and content hugging priorities. Without success, I still feel like they could be a solution (if I knew how to use them correctly).

It doesn’t seem like a complex problem, I can’t find the right Auto Layout API for this. Any hints?


Solution

  • You should set:

    You should add a width constraint as well, if the top and bottom views do not have intrinsic width.

    Demo:

    enter image description here enter image description here