在swift中改变运行时的约束

时间:2015-09-27 04:44:27

标签: swift uiview autolayout show-hide nslayoutconstraint

我在ios的限制部分非常弱。我已经浏览了许多参考链接,如下所示,但仍无法解决我的问题。

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

如果我需要根据某些条件更改UIView2的y轴。 storyboard.png显示了布局。 enter image description here

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

enter image description here

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

中所示

enter image description here

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

2 个答案:

答案 0 :(得分:3)

只需使UIView的高度出口并使用常量属性来改变高度约束,如...

In [4]: def func1():
   ...:     my_list = ['blahblah' for _ in range(100)]
   ...:     my_list[:] = [m for m in my_list if 'blahblah' in m]
   ...:

In [5]: def func2():
   ...:     my_list = ['blahblah' for _ in range(100)]
   ...:     new_list = [m for m in my_list if 'blahblah' in m]
   ...:

In [6]: %timeit func1()
100000 loops, best of 3: 13.9 µs per loop

In [7]: %timeit func2()
100000 loops, best of 3: 13.2 µs per loop

In [8]: %timeit func1()
100000 loops, best of 3: 13.9 µs per loop

In [9]: %timeit func2()
100000 loops, best of 3: 13.2 µs per loop

In [10]: %timeit func1()
100000 loops, best of 3: 13.8 µs per loop

In [11]: %timeit func2()
100000 loops, best of 3: 13.3 µs per loop

答案 1 :(得分:0)

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

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?

相关问题