设置乘数

时间:2016-04-14 10:22:16

标签: ios autolayout uistoryboard

我的故事板中有一个动态高度的辅助视图,这是使布局响应的常见做法。

然而,当我引入乘数时会出现奇怪的事情。

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

enter image description here

将乘数值更改为0.5应该将按钮与白色视图的前半部分的中心对齐,至少在与superview对齐时这样做。

相反,我最终会像这样: enter image description here

蓝色按钮高度等于超视图高度的0.05倍。白色视图高度等于4个时间点蓝色按钮的高度

我不知道这里的问题导致了这种奇怪的对齐。我怀疑它可能是具有动态高度值的东西,所以我尝试设置显式高度值,但结果完全相同。

1 个答案:

答案 0 :(得分:3)

你说过,如果按钮是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 ...

相关问题