UIStackView - 想要一个百分比来定义每个项目

时间:2021-09-01 22:18:19

I want to put two views into a UIStackView. I want the top item to always be 30% of the space no matter what device (or orientation) that it appears. Thus, I want to have the bottom view to be 70%. How do I tell a UIStackView that I want to use a percentage?

我想将两个视图放入UIStackView。我希望顶部项目始终是空间的30%,无论它出现什么设备(或方向)。因此,我希望底部视图为70%。如何告诉UIStackView我想使用百分比?

Example resulting view

示例结果视图

2 个解决方案

#1


10  

Just set a constraint for the top view.

只需为顶视图设置约束即可。

In the storyboard's Document Outline control-drag 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 should see the constraint. Double click on it and set the multiplier to 0.3. Then update the frames.

在storyboard的Document Outline控件中 - 从顶视图拖动到堆栈视图并选择Equal heights。这将使他们的高度相等。现在转到Size Inspector,您应该看到约束。双击它并将乘数设置为0.3。然后更新帧。

If the bottom view doesn't automatically size or it gives you an error telling that you need a height constraint, just repeat the process, but now set the multiplier to 0.7.

如果底部视图没有自动调整大小或者它给出了一个错误,告诉您需要高度约束,只需重复该过程,但现在将乘数设置为0.7。

#2


1  

From apple documentation

来自苹果文档

UIStackViewDistributionFillProportionally A layout where the stack view resizes its arranged views so that they fill the available space along the stack view’s axis. Views are resized proportionally based on their intrinsic content size along the stack view’s axis.

UIStackViewDistributionFillProportionally一种布局,其中堆栈视图调整其排列的视图的大小,以便它们沿堆栈视图的轴填充可用空间。视图根据堆栈视图轴的内在内容大小按比例调整大小。

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

因此,根据这一点,你应该将UIStackView分配属性设置为UIStackViewDistributionFillProportionally并设置其内在大小。

You can get more info here and on Intrinsic content size

您可以在此处获取更多信息以及内在内容大小

#1


10  

Just set a constraint for the top view.

只需为顶视图设置约束即可。

In the storyboard's Document Outline control-drag 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 should see the constraint. Double click on it and set the multiplier to 0.3. Then update the frames.

在storyboard的Document Outline控件中 - 从顶视图拖动到堆栈视图并选择Equal heights。这将使他们的高度相等。现在转到Size Inspector,您应该看到约束。双击它并将乘数设置为0.3。然后更新帧。

If the bottom view doesn't automatically size or it gives you an error telling that you need a height constraint, just repeat the process, but now set the multiplier to 0.7.

如果底部视图没有自动调整大小或者它给出了一个错误,告诉您需要高度约束,只需重复该过程,但现在将乘数设置为0.7。

#2


1  

From apple documentation

来自苹果文档

UIStackViewDistributionFillProportionally A layout where the stack view resizes its arranged views so that they fill the available space along the stack view’s axis. Views are resized proportionally based on their intrinsic content size along the stack view’s axis.

UIStackViewDistributionFillProportionally一种布局,其中堆栈视图调整其排列的视图的大小,以便它们沿堆栈视图的轴填充可用空间。视图根据堆栈视图轴的内在内容大小按比例调整大小。

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

因此,根据这一点,你应该将UIStackView分配属性设置为UIStackViewDistributionFillProportionally并设置其内在大小。

You can get more info here and on Intrinsic content size

您可以在此处获取更多信息以及内在内容大小