5

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.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Arti
  • 7,356
  • 12
  • 57
  • 122
  • 1
    If you have a constraint that says your width should be 200, and that constraint is required, then it will always be 200 points wide. I don't see anything in the constraints you listed that will cause they constraints to switch to your >=250 constraint. You will want to add something like leading and trailing constraints that try to make the box wider if it has the space to do so. – Barlow Tucker Feb 19 '16 at 17:06
  • Only when two constraints conflict will the priority become the tie breaker. Therefore you cannot just have a single constraint of `width >= 250` and expect the size of the image to grow based on the size of the view in the device. You have to add other constraints that would allow auto layout to take this inequality into account with respect to the newly added constraints. –  Aug 22 '17 at 07:08

2 Answers2

5

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

pkamb
  • 33,281
  • 23
  • 160
  • 191
TomCobo
  • 2,886
  • 3
  • 25
  • 43
1

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

Community
  • 1
  • 1
valarMorghulis
  • 302
  • 1
  • 12