问题描述
我有一个简单(我认为)的问题:我有一个的UIImageView
,这是我在故事板上设置多个约束。
I have a simple (I think) problem: I have a UIImageView
, which I have set multiple constraints for in the storyboard.
有时,我会需要禁用的约束,并设置其架
手动,但后来,我会想重新启用这些约束条件,并有视图返回到它是由约束条件决定的。
At times, I'll need to disable the constraints, and set its frame
manually, but later, I'll want to re-enable those constraints and have the view return to where it was as decided by the constraints.
我以为我可以像这样做:
I thought I could do this with something like:
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var constraint: NSLayoutConstraint!
func example() {
//to set its location and size manually
imageView.removeConstraint(constraint)
imageView.frame = CGRectMake(...)
//to cause it to return to its original position
imageView.addConstraint(constraint)
}
通过这一点,但是,我得到的错误 2015年8月26日01:14:55.417约束[18472:923024]视图层次不是prepared的约束:< NSLayoutConstraint :0x7fbb72815f90五:[_ UILayoutGuide:0x7fbb72814c20] - (100) - 的UIImageView:0x7fbb72814540>
。没有人知道为什么这个错误发生?
With this, however, I get the error 2015-08-26 01:14:55.417 Constraints[18472:923024] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fbb72815f90 V:[_UILayoutGuide:0x7fbb72814c20]-(100)-[UIImageView:0x7fbb72814540]>
. Does anyone know why this error occurs?
致电 self.view.addConstraint(...)
摆脱的添加约束,而不是为视图
错误,但仍然不会带来图像视图回到它应该的。
Adding the constraints instead to view
by calling self.view.addConstraint(...)
gets rid of the error, but still doesn't bring the image view back to where it should be.
我发现我甚至没有删除,也没有取消限制,以便能够将的ImageView
的没有问题的框架,只要我不叫 setTranslatesAutoresizingMaskIntoConstraints(真)
。
I notice I don't even have to remove nor deactivate constraints to be able to set the frame of the imageView
without problem, so long as I don't call setTranslatesAutoresizingMaskIntoConstraints(true)
.
我已经看过<一个href=\"http://stackoverflow.com/questions/26021044/programmatically-disable-auto-layout-constraint?answertab=votes#tab-top\">this的问题,但答案似乎不必要的复杂,也许是过时的,甚至是不适用的。
I've already seen this question, but the answer seems unnecessarily complicated, perhaps outdated, and even inapplicable.
我也看了this的问题,但这并不能掩盖约束可以重新启用仅他们如何被禁用。
I also saw this question, but this doesn't cover constraints can be re-enabled—only how they can be disabled.
尝试设置有效
属性:
Trying to set the active
property:
self.top.active = false
self.right.active = false
self.bottom.active = false
self.left.active = false
imageView.frame = CGRectMake(100, 100, 100, 100)
这实际上只是导致的ImageView
是不再可见。 (如果我没有将所有的约束有效
属性设置为false,但的ImageView
移动到合适的位置
This actually just results in imageView
being no longer visible. (If I don't set all the constraints' active
property to false, however, imageView
moves to the proper position.
真正的问题,不过,是当我尝试让图像视图返回它在哪里,所有的约束有效
属性设置为true,没什么发生-的ImageView的保持它是(在由 imageView.frame = CGRectMake指定此情况下(100,100,100,100)
。
The real problem, though, is that when I try to have the image view return to where it was, by setting all the constraints' active
property to true, nothing happens—the imageView stays where it was (in this case as specified by imageView.frame = CGRectMake(100, 100, 100, 100)
.
此外,现在看来,根据我的测试和约阿希姆Bøggild的回答here,当一个约束的有效
属性设置为false时,该约束变为零,因而不能被重新激活。
Also, it seems, according to my testing and Joachim Bøggild's answer here, that when a constraint's active
property is set to false, that constraint becomes nil, and thus cannot be re-activated.
添加约束到一个数组和启用/禁用它们:
有趣的是,这将导致几乎完全同样的问题设置有效
属性。把所有的约束在一个数组阵列
,然后调用 NSLayoutConstraint.deactivateConstraints(阵列)
使得图像视图中消失。此外,重新激活后与 NSLayoutConstraint.activateConstraints(阵列)的限制
没有图像视图返回到它应该,它保持它在哪里。
Interestingly, this causes almost exactly the same issues as setting the active
property. Placing all the constraints in an array array
, then calling NSLayoutConstraint.deactivateConstraints(array)
makes the image view disappear. Also, re-activating the constraints later with NSLayoutConstraint.activateConstraints(array)
doesn't have the image view return to where it should—it stays where it was.
推荐答案
啊哈!
约束将成为零(因此不能被重新激活),一旦他们的有效
属性设置为false。使他们的强引用(感谢,迦勒,为清理命名)preserves他们,使他们可以被激活并根据需要停用。
Constraints become nil (and thus can't be reactivated), once their active
property is set to false. Making them strong references (thanks, Caleb, for clearing up the nomenclature) preserves them so they can be activated and deactivated as desired.
这篇关于启用+禁用自动布局限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!