Le'Kirdok Le'Kirdok - 1 year ago 579
iOS Question

How can i change constraints priority in run time

I have a view which has dynamic height and i am trying to change this view height priority in run time.

Here is my part of code;

if (index == 0) {

surveyViewHeightConstraint.constant = 0;
surveyViewHeightConstraint.priority = 1000;

}else if (index == 1){

surveyViewHeightConstraint.constant = 163;
surveyViewHeightConstraint.priority = 500;


I am changing index with a button action. When i run this code, i am getting this error;

*** Assertion failure in -[NSLayoutConstraint setPriority:], /SourceCache/Foundation/Foundation-1141.1/Layout.subproj/NSLayoutConstraint.m:174

What is my mistake in here? Sorry for my bad english.

Thanks for your answers and advices.

Best regards. :)

Answer Source

As stated in NSLayoutConstraint class reference:

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.

Use priority 999 instead of 1000. It won't be absolutely required technically speaking, but it'll be a higher priority than anything else.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download