在 swift 中在运行时更改约束

changing constraints at runtime in swift

我在ios的约束部分真的很弱。我参考了很多参考资料 link,如下所示,但仍然无法解决我的问题。

Hide autolayout UIView : How to get existing NSLayoutConstraint to update this one

以防万一,我需要根据某些情况更改 UIView2 的 y 轴。 storyboard.png 显示布局。

如果 UIView2 条件在第 1 步失败,第 2 步和第 3 步 应该是可见的 并且它的位置也应该改变,如 condition1.png

所示

如果条件通过 UIView2 的第 1 步,第 2 步和第 3 步 应该不可见,如 condition1.png

所示

请提供实现上述场景的步骤。我尝试这样做但是如果为 UIView1 设置约束高度保持不变并且在运行时不会改变。

只需设置两个 UIView 的高度出口并使用常量 属性 来更改高度限制,例如...

if conditionfails{
     heightconstOfView1.constant = 0
}
else{
    heightconstOfView2.constant = 0
}

我将 outlets 用于故事板上的约束设置,用于按钮之间的 div(使用旧方法设置动态布局)。因此,对于大屏幕(iPhone 6 及以上),我更新了固定高度的值(应用仅在纵向模式下查看)。对我有用的是:

constHeightlogo.constant = 140 //old value = 72
///set any other updates here then force the view
self.view.layoutIfNeeded()

工作得很好。

在此处查看相关内容: