Confused about the direction of iOS constraints
I've been working through Matt Nueberg's Programming iOS 7 book and am struggling to understand something with the direction of constraints. On page 28 of the book, in the multiplier, constant
section, the author states:
The first attribute is set to the result.
So the way I read this, is that when there is a two item constraint, item 1 is derived or dependent based on the value of item 2.
So I have a view and a subview (button), I want to center the button horizontally. So I arrange my view and button and then control-drag from the button to the parent view:
and select Center Horizontally
:
Then if I select that constraint though, I see the following in the attributes inspector at the right:
This shows the superview as item 1, and the button as item 2. Does this mean, that it would try to adjust the center of the superview to match the center of the button? That wouldn't be what I want. Or am I misunderstanding the meaning of item 1 and item 2? Or does it mean that I did the control-drag backwards, that you're supposed to drag from the dependency to the dependent, rather than the dependent to the dependency as I did?
I assume the order does matter, that is, which is item 1 and which is item 2. Because there's a menu option to swap the two. And if it were just a solver with no preference for what was adjusted, that wouldn't be necessary.
You did drag the association the correct way (and that's why the menu option listed centre in container
).
The first item is the one that the constraint is applied to. When you want to centre a view in its container the constraint is applied to the container, not the subview.
Say that you were applying a constraint to link the top of a view to the bottom of the top layout guide. In this case the constraint is applied to the view, so it is item 1 and the layout guide is item 2.
The engine is just a solver, but the order matters in some cases. If 2 items are marked as equal then it doesn't matter which is first. But if the constraint has a multiplier then the order matters - like an aspect ratio constraint where the order dictates what is used as the width and what as the height.
链接地址: http://www.djcxy.com/p/19682.html上一篇: 字体系列中的字体显示大小不同
下一篇: 困惑于iOS约束的方向