ios - 在 anchor 上实际使用乘数可以吗?

标签 ios autolayout constraints

可以在 anchor 和中心上使用乘数吗? 对我来说,至少一开始这根本没有意义,但后来我深入研究了一下,发现这确实有效。

所以让我明白这一点。 如果我错了请告诉我。

1) 前导乘数为 1 = 尾随?我已经看到,以 0.1 的乘数领先,意味着内部有 10%(基于宽度)。

2) 乘数为 2 的中心 = 尾随 ?假设中心意味着尾随/2 或前导 * 0.5 ?

据我所知,乘数实际上乘以基于轴的宽度/高度。

这意味着以下约束是等价的?

C1.axis = C2.axis * M + C <=> C1.axis = C2.axis + axisLength * M + C ?

所以乘数只是加上宽度或高度的倍数?

更新:

因此,使用已接受的答案中的信息来清除问题。

CenterX_or_Y = Width_or_Height / 2
Trailing - Leading = Width  (in therms of size)
Bottom   - Top     = Height (in therms of size)

当您实际在前导/尾随/顶部/底部使用乘数时,您实际上是在上述尺寸(宽度或高度)上使用乘数。

最佳答案

在尺寸上使用倍增器非常简单...我希望我的 subview 是其父 View 宽度的 80%,所以我只设置等宽,倍增器为 0.8

但是,在前导/尾随/中心等上使用乘数时可能会造成混淆。

来自 Apple 的 Auto Layout Guide :

enter image description here

所以,例如,让我们说:

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 的 Leading 是 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

并且,如果 Blue 的 Leading 为 50,则 Blue 的 Trailing 为 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

enter image description here

对于集合 #1,乘数是 1.0 - 对于集合 #2、#3 和 #4,乘数是 0.5

第 1 组,蓝色的 Leading 是 0,绿色的 Multiplier 是 1 ...这是人们通常看到的,而且很明显 - 绿色是 8 分来自 Blue 的 Trailing,即 (Blue.Leading + Blue.Width),或

1.0 * (0 + 100) = 100
100 + 8 = 108

第 2 组,Blue 的 Leading 仍然是 0,但是 Green 的 Multiplier 是 0.5 ... 所以 Green 比 Blue 的 Trailing 高 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 组看起来很奇怪。 Blue 的 Leading 现在是 200,Green 的 Multiplier 仍然是 0.5 ...所以 Green 距离 Blue's Trailing 有 8 pts,即 (Blue.Leading + Blue.Width), * 0.5,或

0.5 * (200 + 100) = 150
150 + 8 = 158

正如我们所见,在第 4 组中,绿色最终成为蓝色的左侧,这是正确的,但并不是那么直观。

关于ios - 在 anchor 上实际使用乘数可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52780013/

相关文章:

ios - 在运行时根据设备更改约束

ios - 如何检查用户是否在 Twitter 上关注您?

ios - 空 UIStackView 导致高度/位置不明确

ios - UICollectionViewCell 自动布局

ios - 自动布局 ios 中 ScrollView subview 的动态高度

generics - F# - 类型参数在 'k : comparison 时缺少约束

ios - Xcode 调试面板不见了

android - xamarin 在整个网格列上形成拉伸(stretch)按钮

ios - SKScene iPad 高度宽度反转

ios - 当应用程序面向横向和纵向时更改不同的 self.view.backgroundColor 图像