Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't understand constraints priority

Tags:

ios

autolayout

enter image description here

I create a rect with following constraints:

enter image description here

I though if i set priority to constraints with width >=250 to 999, then width equal == 200 will work on small devices, and with width >= 250 on large.

But it is not worked. I read documentation:

After solving for the required constraints, Auto Layout tries to solve all the optional constraints in priority order from highest to lowest. If it cannot solve for an optional constraint, it tries to come as close as possible to the desired result, and then moves on to the next constraint.

This combination of inequalities, equalities, and priorities gives you a great amount of flexibility and power. By combining multiple constraints, you can define layouts that dynamically adapt as the size and location of the elements in your user interface change.

like image 660
Arti Avatar asked Feb 19 '16 16:02

Arti


2 Answers

The priority for the constrains will be applied in order to resolve a conflict between two different constraints. The frame's view will be modified applying the constraint with more priority. So, you should have another view or use the superview to apply the priority to the constraints.

This is a nice answer explaining the resistance priority:

Cocoa Autolayout: content hugging vs content compression resistance priority

like image 65
TomCobo Avatar answered Oct 11 '22 07:10

TomCobo


iOS does what you tell. Constraints do not disable automatically depending on screen sizes. Constraints with higher priority will be applied first.

So, when iOS needs to layout your view, it will lookup the specifications that you provided. Now, according to your given specification, the view should have the width of 200 (highest priority) irrespective of screen sizes. So this constraint is applied and you get your view with width of 200.

How can you do what you want?

You should try to disable the constraint with "width" specification of 200, when your app is running on large screen then your other constraint will work and give correct result.

For disabling/enabling constraint -

Stackoverflow Link

Apple Developer Library

like image 24
valarMorghulis Avatar answered Oct 11 '22 08:10

valarMorghulis