Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 850
Le'Kirdok Avatar asked Jul 02 '15 13:07

Le'Kirdok


People also ask

What is priority in constraints?

Ans : Constant priority is a number to determine how important is that constraint. The number can range from 1 to 1000, the higher the number goes, the more important a constraint is. Lower priority in screen seen like dashed blue line. This is useful when two constraint make conflict.

What is Nslayoutconstraint?

The relationship between two user interface objects that must be satisfied by the constraint-based layout system.


2 Answers

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.

like image 144
Cyrille Avatar answered Oct 07 '22 18:10

Cyrille


I want to make a small addition to Cyrille's answer.

If you are creating constraint in code, make sure to set its priority before making it active. For instance:

surveyViewHeightConstraint = [NSLayoutConstraint constraintWithItem:self                                                attribute:NSLayoutAttributeHeight                                                relatedBy:NSLayoutRelationEqual                                                   toItem:self.superview                                                attribute:NSLayoutAttributeHeight                                               multiplier:1                                                 constant:0]; surveyViewHeightConstraint.active = YES; surveyViewHeightConstraint.priority = 999; 

This would result in run-time exception.

Mutating a priority from required to not on an installed constraint (or vice-versa) is not supported.

Correct order is:

surveyViewHeightConstraint.priority = 999; surveyViewHeightConstraint.active = YES; 

For Swift version 4+

constraintName.priority = UILayoutPriority(rawValue: 999) 
like image 30
Vipin Avatar answered Oct 07 '22 16:10

Vipin