【问题标题】:Dynamically set view top margin and height using autolayout - swift3使用自动布局动态设置视图上边距和高度 - swift3
【发布时间】:2018-11-14 21:20:11
【问题描述】:

我正在开发一个有 3 个标签的 UI。全部垂直排列-

标签 1

标签 2

标签 3

  1. 列表项一次将向用户显示任意两个标签。
  2. 例如,如果显示 Label 1Label 3,那么我想将 Label3 上移到 label1 下方并制作 Label2 高度为 0。
    1. 如果显示 Label2Label3,我们必须向上移动两个标签并将 label1 的高度设置为 0。 所以每次 topmst 标签都应该与 superview 具有相同的上边距。它可以是 Label1、Label2 或 Label3

我应该使用堆栈视图来实现这个要求吗?

使用自动布局的最佳方法是什么?

【问题讨论】:

  • 您绝对应该为此使用UIStackView。无论您需要消除哪个标签,您只需将该标签隐藏即可。就是这样。每个其他标签都会相应地调整它们的位置。
  • 是的。它有帮助!

标签: ios autolayout xcode9 swift3.2


【解决方案1】:

您可以在 IBOutletCollection(NSLayoutConstraint) 属性中对不同的场景约束进行分组(每个场景/一组约束一个),例如:

@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *scenarioNConstraints;

然后根据您的条件,您可以通过执行类似的操作来激活或停用欲望约束

for (NSLayoutConstraint *constraint in scenarioNConstraints) {
    [constraint setActive:YES]; //or [constraint setActive:NO];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多