UIStackView - Percent required to define each item

I want to add two views to a UIStackView. I want the top element to always make up 30% of the space no matter what device (or orientation) it appears. So I want the bottom to be 70%. How do I tell UIStackView that I want to use a percentage?

Summary view example

+5
source share
2 answers

Just set a limit for the top view.

In the Document Structure storyboard, drag a control from the top view to the stack view and select Equal heights . This will make their heights equal. Now go to the Size Inspector and you will see a restriction. Double-click on it and set the multiplier to 0.3. Then refresh the frames.

If the bottom view is not automatically changed or it gives you a message that you need a height limit, just repeat this process, but now set the multiplier to 0.7.

+5
source

From Apple documentation

UIStackViewDistributionFillProportionally Layout where the view stack resizes its ordered views so that they fill the available space along the view axis of the stack. Views change proportionally based on their own size of the content along the axis of the stack view.

So, according to this, you should set the UIStackView distribution property to UIStackViewDistributionFillProportionally and set the intrinsic size to it.

You can get more information here and Internal content size

+1
source

All Articles