Enable + Disable Auto-Layout Constraints

雨燕双飞 提交于 2019-12-02 21:55:35

Aha!

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.

Caleb

If you look at the docs for NSLayoutConstraint you'll find a section called Activating and Deactivating Constraints that describes the active property, which in turn tells you:

You can activate or deactivate a constraint by changing this property...Activating or deactivating the constraint calls addConstraint: and removeConstraint: on the view that is the closest common ancestor of the items managed by this constraint. Use this property instead of calling addConstraint: or removeConstraint: directly.

So, once you've got a strong reference to the constraint in question (such as you have with your outlet), you can simply set it's active property to false (or NO in Obj-C) to disable, or true (or YES) to enable.

Connect Constraint as IBOutlet from storyboard to ViewController and create an Array add All constraint in array.

Deactivate those Constraint when not in uses.

Add constraint whenever in uses.

NSLayoutConstraint *constraint1;
NSLayoutConstraint *constraint2;
NSLayoutConstraint *constraint3;
NSArray *arr = @[constraint1,constraint2,constraint3];
 [NSLayoutConstraint deactivateConstraints:arr];

//After deactivate constraint array add what ever frame you want to add

//Activate  lateroN
[NSLayoutConstraint activateConstraints:arr];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!