Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trailing constraint is missing error

I have the following error showing in Xcode and cannot seem to resolve it:

Trailing constraint is missing, which may cause overlapping with other views.

I have tried adding the trailing constraint and also amending the Equal and Greater than or equal settings but neither makes the error go away.

I have also used the 'Reset to Suggested Constraints' and 'Update Constraint Constants' but again with no luck.

I can amend all the items on my view controller but always finish with this error although it can be any of the items and not a specific one which is confusing me even further.

Please see image attached.

I have tried to be as clear as possible but I apologise if I have missed something.

Please can anyone advise how this error can be fixed?

enter image description here

like image 542
user1747021 Avatar asked Jan 09 '18 08:01

user1747021


2 Answers

Xcode 13.3

A nice little workaround for this type of constraints (if you just want them to disappear) is to create a missing constraint (for leading or trailing edge) and then edit it's relation and constant to "greater or equal to 0". This way you can be sure that it won't mess up your view and the warning will disappear:

In this example GPS Logout is missing it's trailing constraint:

enter image description here

Add a missing constraint with the suggested value:

enter image description here

The warning will disappear, but to make sure that this constraint won't affect the view no matter the screen size you can edit it's constant and relation to >= 0

enter image description here

like image 61
Bartosz Kunat Avatar answered Oct 03 '22 08:10

Bartosz Kunat


It's telling you that your labels on the left may intersect with your switches on the right. You need to give your labels a constraint telling them how far they should be from the switches.

You can either give your labels a variable width/set distance I.E. set right constraint from switch. Or you could do a set width/variable distance from the switch by clicking on the label, giving it a right constraint from switch and then making that constraint less than or equal to some value by clicking on the constraint and changing the relation setting.

Another option is to align all your labels with a trailing constraint and then you only have to tell one label how it should constrain to the switches and the others will follow.

EDIT: Another option is to add both the label and corresponding switch to a stack view by selecting both and clicking on the button to the left of the align constraint button. Then horizontally align the stack view, give the stack view a left, right and either top or bottom constraint and that should also fix it. (This is probably what I would do myself).

StackView Example:

StackViewExample

As you can see, clicking the highlighted button makes a stackview that holds child views.

Set Distance and Align Trailing Example:

enter image description here

like image 27
Cameron Porter Avatar answered Oct 03 '22 06:10

Cameron Porter