我什么时候可以激活/关闭布局限制? [英] When can I activate/deactivate layout constraints?
问题描述
我已经设置了多套限制在IB,我想这取决于某些国家在它们之间进行切换编程。有一个 constraintsA
插座收集所有这一切都被标记为从IB安装和 constraintsB
插座收集所有这些都是卸载在IB。
我可以在两个组之间切换编程像这样:
NSLayoutConstraint.deactivateConstraints(constraintsA)
NSLayoutConstraint.activateConstraints(constraintsB)
但是......我想不通的在的做到这一点。好像我应该能够做到在 viewDidLoad中
,有一次,但我不能得到那个工作。我已经打过电话 view.updateConstraints()
和 view.layoutSubviews()
设置约束条件之后,但都没有果。
我也觉得,如果我设置 viewDidLayoutSubviews
的约束一切正常。我想我想知道两件事情...
- 为什么会出现这种现象?
- 是否可以激活/从viewDidLoad中取消限制?
我启用和停用 NSLayoutConstraints
在 viewDidLoad中
和我没有任何问题,它。因此,它的工作。必须有在你的应用程序和我之间设置一个区别: - )
我只是描述我的设置 - 也许它可以给你一个铅:
- 我设置了
@IBOutlets
对,我需要激活/停用 的约束 - 在
的ViewController
,我保存不弱约束成class属性。这样做的原因是,我发现,停用后的约束,我不能重新激活它 - 它是零。因此,似乎失活时被删除。 - 我没有用户
NSLayoutConstraint.deactivate /激活
像你一样,我用constraint.active = YES
/NO
代替。 - 设置限制后,我称之为
view.layoutIfNeeded()
。
I've set up multiple sets of constraints in IB, and I'd like to programmatically toggle between them depending on some state. There's a constraintsA
outlet collection all of which are marked as installed from IB, and a constraintsB
outlet collection all of which are uninstalled in IB.
I can programmatically toggle between the two sets like so:
NSLayoutConstraint.deactivateConstraints(constraintsA)
NSLayoutConstraint.activateConstraints(constraintsB)
But... I can't figure out when to do that. It seems like I should be able to do that once in viewDidLoad
, but I can't get that to work. I've tried calling view.updateConstraints()
and view.layoutSubviews()
after setting the constraints, but to no avail.
I did find that if I set the constraints in viewDidLayoutSubviews
everything works as expected. I guess I'd like to know two things...
- Why am I getting this behavior?
- Is it possible to activate/deactivate constraints from viewDidLoad?
I activate and deactivate NSLayoutConstraints
in viewDidLoad
, and I do not have any problems with it. So it does work. There must be a difference in setup between your app and mine :-)
I'll just describe my setup - maybe it can give you a lead:
- I set up
@IBOutlets
for the constraints that I need to activate/deactivate - In the
ViewController
, I save the constraints into class properties that are not weak. The reason for this is that I found that after deactivating a constraint, I could not reactivate it - it was nil. So it seems to be deleted when deactivated. - I do not user
NSLayoutConstraint.deactivate/activate
like you do, I useconstraint.active = YES
/NO
instead. - After setting the constraints, I call
view.layoutIfNeeded()
.
这篇关于我什么时候可以激活/关闭布局限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!