I add a “align” constraint, but Xcode add a “pin” constraint autolayout

 ̄綄美尐妖づ 提交于 2019-12-25 04:45:29

问题


When we add constraints, we have the choice between "Align" button in the auto layout toolbar or "Pin" button.

In my storyboard, if I add a new alignment constraint "Bottom edges" between a scrollview and his container.

The icon of this constraint is 2 squares and a line.

But after, in the list of constraints of my scrollview, the icon is different :

The icon is a "Pin" icon.

I don't understand why.

One more question, I think it's related : what is the difference between "Align bottom" and "Bottom space" ?


回答1:


Pin and Align are just abstractions to make it easier to understand what you are setting up. In reality, a constraint is a relation between attributes of two objects. What you specify in Interface Builder with align or pin gets translated into an NSLayoutConstraint object.

When done programmatically, there is no pin or align option, you are just specifying the objects, their attributes (.Top, .Bottom, .CenterX, etc.) , the relation (.Equal, .GreaterThanOrEqual, .LessThanOrEqual), a constant, and a multiplier.

For example, here is how to align the bottoms of two buttons:

NSLayoutConstraint(item: button1, attribute: .Bottom, relatedBy: .Equal, toItem: button2, attribute: .Bottom, multiplier: 1.0, constant: 0.0)

If the two objects are siblings, then their bottoms are considered to be aligned. If one object is a subview of the other object, then it is considered to be Bottom Space. Both are just a relation between the .Bottom of one view to the .Bottom of the other view.

When you click on the constraint and view it in the Attributes Inspector, you see the values that are used to create the NSLayoutConstraint.



来源:https://stackoverflow.com/questions/27892626/i-add-a-align-constraint-but-xcode-add-a-pin-constraint-autolayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!