我什么时候可以激活/关闭布局限制? [英] When can I activate/deactivate layout constraints?

查看:245
本文介绍了我什么时候可以激活/关闭布局限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了多套限制在IB,我想这取决于某些国家在它们之间进行切换编程。有一个 constraintsA 插座收集所有这一切都被标记为从IB安装和 constraintsB 插座收集所有这些都是卸载在IB。

我可以在两个组之间切换编程像这样:

  NSLayoutConstraint.deactivateConstraints(constraintsA)
NSLayoutConstraint.activateConstraints(constraintsB)

但是......我想不通的的做到这一点。好像我应该能够做到在 viewDidLoad中,有一次,但我不能得到那个工作。我已经打过电话 view.updateConstraints() view.layoutSubviews()设置约束条件之后,但都没有果。

我也觉得,如果我设置 viewDidLayoutSubviews 的约束一切正常。我想我想知道两件事情...


  1. 为什么会出现这种现象?

  2. 是否可以激活/从viewDidLoad中取消限制?


解决方案

我启用和停用 NSLayoutConstraints viewDidLoad中和我没有任何问题,它。因此,它的工作。必须有在你的应用程序和我之间设置一个区别: - )

我只是描述我的设置 - 也许它可以给你一个铅:


  1. 我设置了​​ @IBOutlets 对,我需要激活/停用
  2. 的约束
  3. 的ViewController ,我保存不弱约束成class属性。这样做的原因是,我发现,停用后的约束,我不能重新激活它 - 它是零。因此,似乎失活时被删除。

  4. 我没有用户 NSLayoutConstraint.deactivate /激活像你一样,我用 constraint.active = YES / NO 代替。

  5. 设置限制后,我称之为 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...

  1. Why am I getting this behavior?
  2. 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:

  1. I set up @IBOutlets for the constraints that I need to activate/deactivate
  2. 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.
  3. I do not user NSLayoutConstraint.deactivate/activate like you do, I use constraint.active = YES/NO instead.
  4. After setting the constraints, I call view.layoutIfNeeded().

这篇关于我什么时候可以激活/关闭布局限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆