在尺寸上使用乘数非常简单...我希望我的子视图是其父视图宽度的 80%,所以我只需使用 0.8 的乘数设置相等宽度
但是,在前导/尾随/中心/等上使用乘数时可能会令人困惑。
来自苹果的Auto Layout Guide:
所以,例如,让我们说:
Red Leading is set to Blue Trailing, Constant 8, Multiplier 1
Blue Leading is at 0, and width is 100
Red's Leading will be (1.0 x 100) + 8 = 108
不过,需要明确的是,Blue 的 Trailing 与其 Width 不同。
假设 Blue 的领先在 50?如果它的 Width 是 100,它的 Trailing 就是 150,所以:
Red's Leading will be (1.0 x 150) + 8 = 158
现在,将 Blue's Leading 放回 0,但让我们将 Multiplier 更改为 0.75
Red's Leading will be (0.75 x 100) + 8 = 83
并且,如果蓝色的领先是 50,那么蓝色的尾随是 150:
Red's Leading will be (0.75 x 150) + 8 = 120.5
只需返回公式即可:
item1.attribute = multiplier * item2.attribute + constant
这里有一个直观的例子来说明。所有标签都是100x40,每个绿色标签都是约束Green.Leading = Blue.Trailing + Constant: 8
对于第 1 组,乘数为 1.0 - 对于第 #2、#3 和 #4 组,乘数为 0.5。
第 1 组,Blue 的领先是 0,Green 的乘数是 1 ...这是人们通常看到的,很明显 - 绿色距离 Blue 的 Trailing 有 8 分,即 (Blue.Leading + Blue.Width),或
1.0 * (0 + 100) = 100
100 + 8 = 108
第 2 组,蓝色的领先仍然是 0,但绿色的乘数是 0.5 ... 所以绿色距离蓝色的尾随 8 分,即 (Blue.Leading + Blue.Width),* 0.5,或
0.5 * (0 + 100) = 50
50 + 8 = 58
第 3 组,蓝色的领先现在是 80,绿色的乘数仍然是 0.5 ...所以绿色距离蓝色的尾随 8 分,即 (Blue.Leading + Blue.Width),* 0.5,或
0.5 * (80 + 100) = 90
90 + 8 = 98
第 4 组看起来很奇怪。蓝色的领先现在是 200,绿色的乘数仍然是 0.5 ...所以绿色距离蓝色的尾随 8 分,即 (Blue.Leading + Blue.Width),* 0.5,或
0.5 * (200 + 100) = 150
150 + 8 = 158
正如我们所见,在第 4 组中,绿色最终成为蓝色的左侧,这是正确的,但并不是那么直观。