Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - View order when creating a constraint

When creating a constraint (programmatically or from interface builder) does it matter the order of the views? Does it matter which is the first item and which is the second item ?

like image 465
Dan Avatar asked Jul 05 '17 07:07

Dan


People also ask

What is priority in constraints iOS?

In the Apple developer documentation, Apple introduced the constraint priority: The layout priority is used to indicate to the constraint-based layout system which constraints are more important, allowing the system to make appropriate tradeoffs when satisfying the constraints of the system as a whole.

What is auto layout in iOS?

Auto Layout constraints allow us to create views that dynamically adjust to different size classes and positions. The constraints will make sure that your views adjust to any size changes without having to manually update frames or positions.

What are two properties that auto layout constraints control on a UIView?

Auto Layout defines margins for each view. These margins describe the preferred spacing between the edge of the view and its subviews. You can access the view's margins using either the layoutMargins or layoutMarginsGuide property.


1 Answers

Yes, it does, because the constant value is added to the second item to determine the value of the first item.

So, if you have label2.leading = label1.trailing + 45 then you get something like:

enter image description here

But if you have label1.trailing = label2.leading + 45 you get:

enter image description here

because the trailing edge of label1 now needs to be 45 away from the leading edge of label2, which means that label2 is on top of label1

If I say label1.trailing = label2.leading + -45 then I will have the same as in the first instance: enter image description here

You would have similar issues with top/bottom constraint items too.

If you select Reverse first and second items in Interface Builder, then it will switch the sign of the constant for you. If you are doing it programatically then you need to account for this yourself.

like image 87
Paulw11 Avatar answered Sep 29 '22 01:09

Paulw11