简体   繁体   English

如何理解自动布局约束的第一项和第二项?

[英]How to understand auto layout constraint's first item & second Item?

If I use a tableView as a View's subView in xib. 如果我在xib中使用tableView作为View的subView。 And the tableView is fully covered the View. 并且tableView完全覆盖了View。 but in the xib's attributor , i can see the two vertical constrains just added : the 1st constraint use tableView's top as first Item , superView's top as second item , But in the 2nd constraint, it show that superview's bottom was the first item , and the tableView's bottom was the second item . 但是在xib的attributor中,我可以看到刚添加的两个垂直约束:第一个约束使用tableView的top作为第一个项目,superView的top作为第二个项目,但是在第二个约束中,它显示superview的底部是第一个项目,并且tableView的底部是第二个项目。 what is the reason of the xib to decide use who as the first item & second Item . 是什么原因让xib决定使用谁作为第一项和第二项。

All constraints express a formula of the form: 所有约束都表达了以下形式的公式:

firstItem.firstItemAttribute == secondItem.secondItemAttribute * multiplier + constant

(The relation could be <= or >= , too.) (关系也可以是<=>= 。)

Most often, the multiplier is 1, so drop that: 大多数情况下,乘数是1,所以下降:

firstItem.firstItemAttribute == secondItem.secondItemAttribute + constant

It's usually easiest to think in terms of positive constants. 从正常数的角度来考虑通常最容易。 So, something like: 所以,像:

view.Top == superview.Top + 20

makes sense. 说得通。 The view's top is 20 points below the superview's top. 该视图的顶部比superview的顶部低20分。 (In the coordinate system used by auto layout, Y increases down, so adding 20 points gives a position "below".) (在自动布局使用的坐标系中,Y增加,因此添加20个点会使位置“低于”。)

You could switch the items around, but, if you want the same relationship, you'll need to negate the constant: 你可以切换周围的项目,但是,如果你想要相同的关系,你需要否定常量:

superview.Top == view.Top + -20

That -20 may make it harder to understand. -20可能会让人更难理解。 By the way, Xcode will happily swap the two items and negate the constant for you. 顺便说一句,Xcode会愉快地交换这两个项目并为你否定常数。 (If the multiplier weren't 1, the math gets a bit more complicated, but it's still possible.) (如果乘数不是1,数学会变得有点复杂,但它仍然可能。)

If you were to just swap the items around but not negate the constant, it would express a different relationship: 如果你只是交换周围的项目而不是否定常数,它会表达一种不同的关系:

superview.Top == view.Top + 20

That would mean that the superview's top would be below the view's top. 这意味着superview的顶部将低于视图的顶部。 Or, rather, that the view would be positioned above the top of the superview and it's top would be clipped. 或者更确切地说,视图将位于超视图的顶部上方,并且顶部将被剪裁。

Now, constraints on the other end are most often expressed with items in the other order because you usually want the relationship to be the opposite. 现在,对另一端的约束通常用另一个顺序的项表示,因为你通常希望关系是相反的。 That is, you want the view's top to be below the superview's top, but you want the view's bottom to be above the superview's bottom. 也就是说,您希望视图的顶部位于superview的顶部下方,但您希望视图的底部位于superview的底部之上 So, the constant is positive only when you arrange the items in the other order: 因此,只有当您按其他顺序排列项目时,常量才是正数:

superview.Bottom == view.Bottom + 20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自动布局约束第一项和第二项如何工作? - How Autolayout constraint First Item and Second Item works? 约束必须包含第一个布局项 NSLayoutConstraint - Constraint must contain a first layout item NSLayoutConstraint 变更约束第二项 - Change constraint second item UITableViewCell的NSLayoutConstraint:约束必须包含第一个布局项 - NSLayoutConstraint for UITableViewCell: Constraint must contain a first layout item 如何使用自动布局来布局水平相等的三个项目 - How to Use auto layout to layout three item horizontal equal 如何以编程方式将顶部约束更改为布局中的另一个项目 - How can I change a top constraint to another item in the layout programatically 如何将表格视图行文本从第一个标签项推到第二个标签项的标签? - How can I push table view row text from first tab item to second tab item's label? 首次加载时自动选择项目 - auto selected Item at first load 目标 c 如何根据 select 第一个文本字段项删除第二个和第三个文本字段中的项目 - objective c how to remove the item in second and third text field based on select the first text field item 如何理解 Swift UIKit 中的自动布局? - How to understand Auto layout in Swift UIKit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM