Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface Builder - Constraint maximum width and margins

I have a constraint error,

What I want to do is "center the view, have 20 margin on the view, and have a maximum width of 320"

So that on iPad the view is not stretched, but on smaller devices the view can be less than 320 width, and have 20 pts of margin.

However using these rules gives me an error, probably because I say that margins can be >= 20, and width <= 320, and view has no idea how to match that.

I tried using priority but I'm not sure how it works, and I couldn't get anything.

How can I achieve the desired effect using only one view and its constraints?

enter image description here

like image 215
htafoya Avatar asked Jan 14 '17 19:01

htafoya


1 Answers

As you have deduced, autolayout needs to know which constraint it should break in the event of a conflict, and this is done by priorities. However, you have another conflict since the leading/trailing and width constraints are all relative. This means that autolayout cannot determine the view's width.

Change the leading/trailing constraints to = 20 and set their priority to 999 so they can be broken on a wider device. This will allow the width to be determined and eliminate your conflicts.

like image 50
Paulw11 Avatar answered Sep 21 '22 06:09

Paulw11