【问题标题】:Stack view arrangement of controls percentage wise in iOS swiftiOS swift中控件百分比的堆栈视图排列
【发布时间】:2016-09-07 19:53:37
【问题描述】:

我正在构建一个带有表单的 iOS 应用程序,并且我正在使用 stackviews。这是我想要实现的示例:

文本框和按钮是水平堆栈视图的一部分。我需要的是一种为它们分配类似百分比的宽度的方法。例如,文本框将占用堆栈视图总宽度的 80%,而按钮将占用 20%。

Stackview 的对齐和分布属性设置为“填充”。

我尝试为按钮添加宽度限制,但我觉得这样做的方法不正确,因为不同屏幕尺寸的宽度会有所不同。

【问题讨论】:

  • 澄清一下,您在此处使用水平堆栈视图进行基本静态布局,即您始终显示这两个项目。

标签: ios swift uistackview


【解决方案1】:

您可以使用“等宽”属性并为子视图分配各自的乘数 0.80 和 0.20。

请检查以下步骤:

1. Select textbox and Stackview and assign "Equal width" contraints to it.
2. Then select the contraints and edit it by applying multiplier of 0.80 to the textbox contraints.
3. Then select button and Stackview and assign "Equal Width" contraints to it.
4. Then select the contraints and edit it by applying multiplier of 0.20 to the button contraints.

使用上述解决方案,宽度将按百分比分布在所有屏幕尺寸中。

有关更多信息,请参阅此页面: https://jascode.wordpress.com/2019/08/15/weight-assignment-in-stack-viewpercentage-wise-size-calculation/

【讨论】:

    【解决方案2】:

    您正在寻找UIStackViewDistributionFillProportionally 设置。

    这将使用视图的内在内容大小之间的比例作为其实际大小之间的比例。因此,对于 80% 到 20%,将一个视图设为 80 宽,一个视图设为 20 宽(在它们的 intrinsicContentSize 覆盖中),它们将按照 80 到 20 的比例调整大小。

    【讨论】:

    • 这有时会有所帮助,但不能保证是固定宽度。如果内容在文本框上展开,按钮会缩短。
    • @RujootaShah 如果发生这种情况,那么听起来您没有正确覆盖内在内容大小。
    猜你喜欢
    • 1970-01-01
    • 2020-07-22
    • 2021-09-07
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多