设置乘数时自动布局中心 y 对齐奇怪的行为

Autolayout center y alignment strange behaviour when setting multiplier

我的情节提要中有一个具有动态高度的辅助视图,这是使布局响应式的常见做法。

然而,当我引入乘数时,发生了一些奇怪的事情。

蓝色按钮与左侧白色视图的中心 y 对齐:

将乘数值更改为 0.5 应该将按钮对齐到白色视图前半部分的中心,至少在对齐到超级视图时它是这样工作的。

相反,我最终得到这样的结果:

蓝色按钮高度等于父视图高度的0.05倍。白色视图高度等于蓝色按钮高度的 4 倍

我不知道是什么问题导致了这种奇怪的对齐。我怀疑它可能是动态高度值,所以我尝试设置显式高度值但结果完全一样。

你说如果按钮 Center Y 乘数 0.5,那么它应该位于那个白色视图的 1/4 处。不,那不是那样工作的。让我们检查一下它与等式

乘数适用于这个方程

 FirstItem.Attribute1 = (SecondItem.Attribute2 * Multiplier) + Constant

你的约束是 Button.Center Y = BlankView.center Y .. 所以这就是方程式的填充方式

 Button.Center Y = (BlankView.center Y * 1) + 0 

所以问题是 BlankView.Center Y 的值是多少... 答案是

 BlankView.Center Y = HeightOfSuperviewOFBlankView - (Y positionOFBlankView + (BlankViewHeight / 2))
 // in your case it would be 603 - (483 +(120/ 2)) = 543

现在移动到你的等式 Center Y 乘数 0.5

 Button.Center Y = (543 * 0.5) + 0  // 271.5

因为你的 Y 中心按钮位置位于

 Button.Center Y = 271.5 - (buttonHeight / 2) 
  // if we take buttonHeight = 30 than it should be 257.5 (approx 257)

我希望您现在了解中心 Y 和乘数的工作原理...