简体   繁体   中英

Auto Layout Reversed Constraints in XCode 6.1

This has been an ongoing issue for me and other people I work with.

Someone would expect that the following two constraints would both work since essentially they are the same constraint.

The "end" of Sign Up and "start" of Login should be separated by 15 points

sounds the same as

The "start" of Login and "end" of Sign Up should be separated by 15 points.

Yet, sometimes you have to inverse the constraint to make it work.

Should I file a radar or have we been missing something?

This is on XCode 6.1 but has been going on for sometime now (can't remember the version).

在此输入图像描述

You've been missing something.

Constraints express an equation:

firstItem.firstAttribute == secondItem.secondAttribute * multiplier + constant

(The equation could also use <= or >= instead of ==.)

Swapping the item-attribute terms does not result in the same equation. You would have to negate the constant to maintain the meaning. (You would also have to divide by the multiplier.)

Your left-side screenshot means:

Sign Up.Trailing == Login.Leading * 1 + 15

Your right-side screenshot means:

Login.Leading == Sign Up.Trailing * 1 + 15

Subtract 15 from both sides of the latter yields:

Login.Leading - 15 == Sign Up.Trailing * 1

Drop the multiplication by 1:

Login.Leading - 15 == Sign Up.Trailing

Swap the two sides:

Sign Up.Trailing == Login.Leading - 15

Re-express in the standard constraint form:

Sign Up.Trailing == Login.Leading * 1 + (-15)

Compare to the left-side:

 Left side: Sign Up.Trailing == Login.Leading * 1 + 15
Right side: Sign Up.Trailing == Login.Leading * 1 + (-15)

They mean different things.

Put another way, the constant is not just a magnitude of the distance, but also the direction, so sign matters. It's very possible to force two views to overlap along a given axis, for example.

它们不一样,因为你的常数是15.你需要将第二个约束中的常量改为-15。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM