How to understand the automatic layout restriction of the first element and second element?

If I use tableView as a subView view in xib. And the tableView fully covers the view. but in the xib attribute, I see that there are only two vertical restrictions added: the first restriction uses tableView from above as the first element, superView top as the second element, but in the second restriction it shows that the lower lower level was the first element, and the Lower table table was second. what is the reason xib decides to use whom as the first element and second element.

+6
source share
1 answer

All restrictions express a formula of the form:

firstItem.firstItemAttribute == secondItem.secondItemAttribute * multiplier + constant 

(The ratio can also be <= or >= .)

Most often, the multiplier is 1, so remove it:

 firstItem.firstItemAttribute == secondItem.secondItemAttribute + constant 

It is usually easier to think in terms of positive constants. So something like:

 view.Top == superview.Top + 20 

has the meaning. The top of the top is 20 points below the top panel. (In the coordinate system used by automatic layout, Y increases downward, so adding 20 points gives the position "lower.")

You can switch elements around, but if you need the same relationship, you will need to negate the constant:

 superview.Top == view.Top + -20 

That -20 may make it harder to understand. By the way, Xcode will happily replace two elements and negate the constant for you. (If the factor was not 1, the math gets a little more complicated, but it is still possible.)

If you simply changed objects around, but did not deny the constant, this would express a different attitude:

 superview.Top == view.Top + 20 

This would mean that the top of the supervisor would be below the top of the view. Or rather, that the view will be located above the top of the supervisor, and the top will be cropped.

Now the constraints on the other end are most often expressed by elements in a different order, because you usually want the relationship to be the opposite. That is, you want the top of the top level to be below the top level of the top, but you want the bottom of the view to be higher than the bottom bottom panel. Thus, a constant is positive only when you order the elements in a different order:

 superview.Bottom == view.Bottom + 20 
+14
source

Source: https://habr.com/ru/post/981588/


All Articles