Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

ios - Difference between leftAnchor and leadingAnchor?

I have been working with constraints in Swift for iOS, and most of the naming conventions/documentation make things quite self explanatory, but one thing that has recently confused me is two pairs of the NSLayoutXAxisAnchor class:

leadingAnchor, leftAnchor

along with

trailingAnchor, rightAnchor

When I have set constraints programmatically in my iOS app, I have found that I can use these totally interchangeably. If I use leadingAnchor, run the app and observe the behavior, and then use leftAnchor, run the app and observe the behavior, it functions exactly the same.

I checked out the documentation from developer.apple.com, and the results are even exactly the same (ignoring the difference in names). For example comparing the leadingAnchor and leftAnchor:

"Use this anchor to create constraints with the view’s leading edge. You can only combine this anchor with a subset of the NSLayoutXAxisAnchor anchors. You can combine a leadingAnchor with another leadingAnchor, a trailingAnchor, or a centerXAnchor. For more information, see NSLayoutAnchor Class Reference."

compare to:

"Use this anchor to create constraints with the view’s left edge. You can only combine this anchor with a subset of the NSLayoutXAxisAnchor anchors. You can combine a leftAnchor with another leftAnchor, a rightAnchor, or a centerXAnchor. For more information, see NSLayoutAnchor Class Reference."

Does anyone know if there is any intended difference between these properties?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This was covered (briefly) in one of the WWDC 2015 videos on the Mysteries of Autolayout (I think it was part 1 but both are worth watching).

Left and Right constraints are absolute, they will always refer to the left/right of the screen or the control. Leading and trailing constraints are affected by the device locale; In locales where the reading direction is left to right (English, French, Spanish and so on) leading & left (and trailing & right) can be used interchangeably. In locales where the reading direction is right to left (e.g Hebrew, Arabic) then 'leading' will be the right side and 'trailing' will be the left side.

The advice in the video was you should almost always use leading/trailing rather than left/right unless you have a specific requirement for absolute left/right.

Consider the typical 'form' of a label and a text field. If you use leading/trailing constraints then you will get the label on the left and text field on the right for an English locale and label on the right, text field on the left for a Hebrew locale.

If you made a children's app where you were teaching left from right and you always wanted the 'left' button on the left of the screen then left/right constraints would be appropriate


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...