在运行时快速更改约束条件

问题描述:

我在ios的约束部分中确实很弱。我已经通过了许多参考链接,如下所示,但仍然无法解决我的问题。在运行时快速更改约束条件

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

在情况下,我需要改变基于一些条件UIView2的y轴。 storyboard.png显示布局。 enter image description here

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

enter image description here

如果条件通过UIView2与步骤1,步骤2和步骤3 应该不可见如condition1.png所示

enter image description here

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

只是使双方的UIView的高度的出口和利用不变的性质改变,如高度限制...

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

让我尝试将更新它.. – cybergeeeek

+0

确定...如果你有任何问题,让我知道 –

+0

高度的视图1的变化,但视图2完全不动。 – cybergeeeek

我用网点上的脚本中设置约束,按键之间的div(使用旧的设置动态布局的方法)。因此,对于大屏幕(iPhone 6及以上版本),我更新固定高度的值(仅在纵向模式下观看应用程序)。对我来说有效的是:

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

工作就像一个魅力。

在这里看到相关:

Update the constant property of a constraint programmatically in Swift?