汽车布局了解乘数使用相对定位 [英] Understanding multiplier in auto layout to use relative positioning

查看:157
本文介绍了汽车布局了解乘数使用相对定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何能够利用自动布局相对于其他视图个明智的位置的项目。

I am trying to understand how one can utilize Auto Layout to position items relative to other views percentage-wise.

例如,最近我了解到,你可以指定一个视图的底部应该位于比它的父的底部高出4%,通过使用这样的:

For example, I recently learned that you can specify a view's bottom should lie 4% higher than its superview's bottom by using this:

self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 0.96, constant: 0))

这对我来说很有意义,因为1乘数会在右侧视图的底部对齐,这样你就可以了4%,通过改变乘数0.96减少量。

This makes sense to me because a multiplier of 1 would align it right at the view's bottom, so you can decrease that amount by 4 percent by changing the multiplier to 0.96.

但你怎么能这样做在另一个方向?要指定一个标签的顶部应该从上海华顶尖开始4%有所下降。如果您使用的同一行,但改变属性为 .TOP ,这意味着它会比上海华顶级高出4%(但它实际上并没有将它关闭屏幕)。你不能有一个负的乘数我不认为,我不相信,大于1的值做任何事情时,恒为0。那么,你如何得到建立?

But how can you do the same in the other direction? You want to specify a label's top should begin 4% down from the superview's top. If you use that same line but change the attributes to .Top, that means it would be 4% higher than the superview's top (but it actually doesn't move it off screen). You can't have a negative multiplier I don't think, and I don't believe a value greater than 1 does anything when constant is 0. So how do you get that set up?

我对实施开头和结尾同样的问题。尾随很容易。如果你想让它10%的右:

I have the same question for implementing leading and trailing. Trailing is easy. If you want it 10% from the right:

self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Trailing, relatedBy: .Equal, toItem: self.view, attribute: .Trailing, multiplier: 0.9, constant: 0))

由于您拨回0.1或10%而不是1.0完全对准这是有道理的。但你如何领导做?我想你也许可以设置标签的龙头相对于视图的结尾,那么乘数为0.1。在我的心目中,这将意味着该标签将开始在很远的权利,但随后回拨90%,因此从左边获得所需的10%。但是,这并非如此,我不知道为什么。

It makes sense because you dial it back 0.1 or 10% instead of aligning fully at 1.0. But how do you do the same for leading? I thought you might be able to set the label's leading relative to the view's trailing, then set the multiplier to 0.1. In my mind that would mean the label would start at the very far right but then be dialed back 90%, therefore obtaining the desired 10% from the left. But that's not the case, I'm not sure why.

您能解释一下这是怎么工作,如何正确地使用乘数来获得这些相对的布局?

Can you explain how this is works, how to properly use multiplier to obtain these relative layouts?

为了方便,假设你想创建一个具有顶部和底部的上海华的身高10%的标签,尾随和领导上海华宽度的10%。在iPhone上纵向有将更多的填充上方和下方的标签比有填充到左边和右边,像这样(是的,它是按比例绘制的):结果

搜索结果但是,让我们说,在iPad上它将会在这是一个完美的正方形,以便显示。因此,填充将是相同的数额周围的一切,就像这样:结果

搜索结果的问题是你如何定义这样的约束是在价值的动态,而不是设定一个恒定的固定值。我已经知道该怎么做底部和尾部,但顶部和领导有我难住了。我希望了解如何使用乘数做更高级的布局,例如,指定一个标签的顶部应在于10%的另一个标签的底部下方,而不是将其设置为一个固定的常数。

To make it easy, let's say you'd like to create a label that has top and bottom 10% of the superview's height, and trailing and leading 10% of the superview's width. On an iPhone in portrait there's going to be more padding above and below the label than there is padding to the left and right of it, like so (yes it's drawn to scale):


But let's say on the iPad it's going to be shown in a view that's a perfect square. Therefore the padding will be the same amount all around, like so:


The question is how do you define such constraints to be dynamic in value, as opposed to setting a fixed value for a constant. I already know how to do bottom and trailing, but top and leading has me stumped. I'm hoping to understand how to use multiplier to do more advanced layouts, for example, specifying a label's top should lie 10% beneath another label's bottom, as opposed to setting it to a fixed constant.

推荐答案

有几个方法可以做到这一点。在最简单的情况下,你已经几乎得到了它:如果你想要的水平边界是在10%和90%,那么你需要指定的两个的相对的后缘限制上海华 - 这样 Subview.Trailing Superview.Trailing 的乘数0.9 ,照你这么说,但随后 Subview.Leading 也锁定至 Superview.Trailing ,只有事半功倍的 0.1

There are a couple ways to do this. In the simplest case, you've already almost got it: if you want the horizontal boundaries to be at 10% and 90%, then you need to specify both constraints with respect to the trailing edge of the superview -- so Subview.Trailing locks to Superview.Trailing with a multiplier of 0.9, as you say, but then Subview.Leading also locks to Superview.Trailing, just with a multiplier of 0.1:

(同样为上/下)

在另一方面,你提到在年底的情况是有点复杂:指定标签的顶部应在于10%的另一个标签的底部下方。对于您可能不能够使用一定比例的插图,如previous情况。相反,你可以在它们之间创建一个无形的间隔观点:添加约束与 Spacer.Height = 0.1 * Superview.Height ,然后将其固定在两个标签之间。 (您也可以用这些垫片意见处理previous情况,但这种情况下,它是不是真的有必要。)

On the other hand, the case you mention at the end is a little more complicated: "specifying a label's top should lie 10% beneath another label's bottom." For that you probably won't be able to use fixed percentage insets like the previous case. Instead, you can create an invisible spacer view between them: add a constraint with Spacer.Height = 0.1 * Superview.Height and then attach it between the two labels. (You can also handle the previous case with these spacer views, but for that case it isn't really necessary.)

这篇关于汽车布局了解乘数使用相对定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆