Difference between leftAnchor and leadingAnchor? - ios

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?

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

Related

Programatically add "Spacing to nearest neighbour" constraint

Is there a way to add a "Spacing to nearest neighbour" constraint programmatically? I saw a similar question posted but I don't think it was in Swift (I'm a beginner when it comes to developing iOS apps).
I haven't tried anything yet as I'm not sure where to start, but thought it might be something along the lines of:
view.bottomAnchor.constraint(equalTo: nearestNeighbour.bottomAnchor, constant: 100).isActive = true
I want to have it such that the scrollView I have in the top half of the screen is a certain distance from the nearest button below. I can't constrain it to the specific button though because I don't have IBOutlets and would rather not add them.
I can't constrain it to the specific button though because I don't have IBOutlets and would rather not add them
Then you are totally stuck. Your requirements are self contradictory. Without a reference to the button of some kind, you cannot possibly make a constraint to it programmatically. There is no “whoever my nearest neighbor is” reference.

Xcode 8 : Auto layout Leading & Trailing

I started iOS development a week ago and I'm currently learning AutoLayout. and sadly I can't wrap my head around the Leading & Trailing constraints and when to use them, I don't even understand them at all. I have done some research and I'm just actually more confused now. Can someone give some examples?
This definition is form the Apple website:
The values increase as you move towards the trailing edge. For a
left-to-right layout directions, the values increase as you move to
the right. For a right-to-left layout direction, the values increase
as you move left.
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/AnatomyofaConstraint.html
Thanks
TL;DR
They couldn't just name it left and right because it had to make sense in both layout orientations: left-to-right and right-to-left. Hence Trailing and Leading.
Longer Answer
I think the question you're asking yourself is why the heck Apple is not just naming it left and right. What's with the trailing and leading.
The reason behind this is that there might be 2 different layouts. Starting with iOS 9, the UI layout for left-to-right languages (like English) is.. well left-to-right. But in case of Arabic for example, it's right-to-left.
However Autolayout is smart enough that you don't need to setup your layout twice for these 2 types of layouts. You just set it up once and the system auto-inverts it in case your app supports right-to-left languages.
For this answer, ignore languages like Arabic and Hebrew that flow right to left. I'm going to answer for the majority of languages that flow left to right.
For those languages, the "leading" constraint determines the space on the left of an object, and the "trailing" constraint determines the space on the right side. (A good mnemonic for this is that left and leading both start with "L".)
For languages that flow the opposite way, the sides are switched, but the "leading" still pins an object to the leading edge (The side where you start reading.)
Leading means the starting point of the view and trailing means the ending point of the view.
For example, if a view has a frame (x: 0, y: 0, width: 100, height: 100) its leading will me 0 and trailing will be 100.
So if you move right from the leading the distance between the leading and the point moved will increase and similarly this happens in case of trailing.
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) 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.
Check the original answer here: Difference between leftAnchor and leadingAnchor?

Auto Layout: understanding the behavior of attributes and items

I use Auto Layout programmatically a lot but there are still two things that I do not understand.
What is the difference between leading and left attribute/anchor and between trailing and right attribute/anchor?
To me there seem to be the same but according to the docs:
However, Auto Layout does not allow constraints that mix leading and trailing attributes with left or right attributes. As a result, this constraint crashes at runtime.
The order of the the items inside the NSLayoutConstraint does not matter for the Auto Layout system at all, am I right? (At least for .Equal relation.)
Do not get confused about the attributes for each individual item, I'm aware of that.
Left and right are always left and right.
Leading and trailing are not. From Working with Constraints in Interface Builder:
If you lay out your views using leading and trailing constraints, the views automatically flip positions when switching between left-to-right languages (like English) and right-to-left languages (like Arabic). However, some interface elements should not change their position based on the reading direction. For example, buttons that are based on physical directions (up, down, left, and right) should always stay in the same relative orientation.
The order of items for an equal relation is irrelevant.
The difference between leading and left is important when taking into account RTL languages, such as Hebrew. In that case, the UI is inverted to complement text direction, and so are the UI elements with constraints. In this case, leading means the side where text starts. left is always left, for text direction agnostic UIs, such as media playback controls.
The order constraints are placed in does not matter. If there are conflicting constraints, the behavior is undefined.

Understanding "firstBaseLine" vs. "baseLine" AutoLayout Constraint

I've created the following view that looks as follow:
And used Add missing constraints t automatically add constraints.
I am pretty new in iOS world and I'm trying to configure out how constraints work.
On document outline, I can see the added constraints:
I click on label UR and Size inspector show me following:
What is the difference between constraints aligned by first baseline to UL and aligned by baseline to UL? When I click on the size inspector on constraints button, it shows me the same line.
From the Apple Docs:
NSLayoutAttributeBaseline
The object’s baseline.
Available in iOS 6.0 and later.
NSLayoutAttributeFirstBaseline
The object’s baseline. For objects with more than one line of text, this is the baseline for the topmost line of text.
Available in iOS 8.0 and later.
Here is also a screenshot of the difference when aligning to a three line label. The upper green label is using NSLayoutAttributeFirstBaseline and the lower green label is using NSLayoutAttributeBaseline. These are the only constraints on the two green labels besides they are both Horizontally pinned 40 from the Pink Label
I don't recommend ever using "Add Missing Constraints." It is a very unpredictable wizard that DWIW (does what it wants). Learn each constraint and add it by hand. Ctrl-drag between objects in the scene hierarchy; use the constraints inspector and edit numeric values; use 'Update Frames' from the triangle pop-up; delete constraints from the inspector; repeat until you get it.
firstBaseLine refers to the top baseline of a multi-line object:
Storyboard warning : First baseline layout attribute before iOS 8.0
if the label or text field involved only has one line, it's a useless constraint.
When you want two side-by-side one-line blotches of text to align vertically, even though their font sizes, ascenders, and baseline descenders may differ, use baseLine alignment. It will look natural like the strings are on the same line.
Bottom, Top, and Center vertical alignments are also possible and more appropriate for non-text objects.
In the image you can see a simple visual example using both baseline constraint.
It is quite illustrator.

Difference between NSLayoutAttributeLeft vs NSLayoutAttributeLeading

What is the difference between NSLayoutAttributeLeft and NSLayoutAttributeLeading in iOS autolayouts?
"Leading" does not always mean "Left".
For RTL-written languages (locales) leading edge of the object’s alignment rectangle will be located at the right side of the object.
Quote from Auto Layout Guide:
The attributes leading and trailing are the same as left and right for left-to-right languages such as English, but in a right-to-left environment such as Hebrew or Arabic, leading and trailing are the same as right and left. When you create constraints, leading and trailing are the default values. You should usually use leading and trailing to make sure your interface is laid out appropriately in all languages unless you’re making constraints that should remain the same regardless of language (such as the order of master and detail panes in a split view).

Resources