简体   繁体   English

自动布局在XCode 6.1中反转约束

[英]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 注册的“结束”和登录的“开始”应该相隔15分

sounds the same as 听起来像是一样的

The "start" of Login and "end" of Sign Up should be separated by 15 points. 登录的“开始”和注册的“结束”应该相隔15分。

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). 这是在XCode 6.1上,但现在已经进行了一段时间(不记得版本)。

在此输入图像描述

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: 从后者的两边减去15:

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

Drop the multiplication by 1: 将乘法乘以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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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