NSLayoutConstraint activate/deactivate

NSLayoutConstraint activate/deactivate

我只是想停用 textViewDidBeginEditing 中的约束和停用 textViewDidEndEditing 中的约束。

我的 NSLayoutConstraint 是一个插座。 textViewDidBeginEditing 中的停用有效,但随后 textViewDidEndEditing 上的重新激活发现约束为 nil。

我已经尝试使用 .isActive 实例值和 NSLayoutConstraint。activate/deactivate 函数。

答案 我尝试了 Vasil Hristov 的回答,但没有用,因为他只是建议 0 和 1000。来自文档:

Priorities may not change from nonrequired to required, or from required to nonrequired. An exception will be thrown if a priority of NSLayoutPriorityRequired in OS X or UILayoutPriorityRequired in iOS is changed to a lower priority, or if a lower priority is changed to a required priority after the constraints is added to a view. Changing from one optional priority to another optional priority is allowed even after the constraint is installed on a view.

所以,我必须首先将 IB 中的优先级更改为其他值(我选择 750 - 高),然后在不需要时将其切换为 250,并创建与第一个约束冲突的第二个约束始终具有 500 优先级(做其他事情),这让我得到了我想要的行为。

根据上述文档,由于不需要 High,我应该可以切换到 0,但这仍然导致崩溃,所以我一定是读错了。因此,为什么我做了第二个约束。

您可以尝试更改约束的优先级。 例如:

constraint.priority = 0;    // it's turn off
constraint.priority = 1000; // it's turn on