📜 ⬆️ ⬇️

Basics of Auto Layout - Concept, structure, application

Auto Layout is engaged in dynamic calculation of the position and size of all views in the view hierarchy, based on the constraints - the rules defined for a particular view . The biggest and obvious advantage for the developer in using Auto Layout is that there is no need to adjust the size of the application for certain devices - Auto Layout does it for you, dynamically changing the interface depending on external or internal changes.

An example of external changes might be: Changing the window size at macOS, changing the screen orientation, various screen sizes.

Example of internal changes: Changing the content in the window, changing depending on the language, etc.
')
You can create your own interface in 3 ways: programmatically, based on a mask that automatically adjusts to changes or use Auto Layout.

The difference between Auto Layout and other methods is that you no longer need to write code that changes the interface depending on the size of the window and other elements; instead, Auto Layout automatically calculates the location of the interface element in the application and changes it relative to the environment.

Unlimited Auto Layout


If for some reason you do not want to use rules ( constraints ) or your interface contains many elements whose arrangement you can change forever, the Stack View will come to your aid.

Stack View is your magic wand when creating complex interfaces. He can arrange the elements inside with these parameters:

axis (UIStackView only) —determines the orientation, horizontally or vertically;
orientation (only NSStackView) - the same as the axis of UIStackView ;
distribution - determines the location of elements in a given orientation;
alignment - determines the location of elements perpendicular to the orientation of the StackView;
spacing - determines the distance between adjacent elements;

For the most satisfactory results, you can use constraints in the StackView itself, or you can nest several StackViews in a StackView and then use constraints, for example, to center the screen.

Anatomy of constraint


The whole essence of the rules comes down to creating a calculation that can have only one answer - the location of the interface element.

It looks like this:

Button. Top = Highest Point of Interface. Low + 100

According to this expression, it is clear what it means and what rule sets for the view . As already mentioned, Auto Layout calculations always occur relative to the nearest elements, be it the edge of the screen or the adjacent button.

In their calculations, constraints use multipliers, nearest objects, and constants like + 100 from the example above. Also, when creating rules it is not necessary that these are equal, you can use> = or <=.

When creating a layout, it is advisable to specify two rules for each dimension per element. But do not forget about StackView, which will help you a lot when creating an interface.

The most interesting fact is that when creating constraints you can prioritize the constraints themselves. When calculating, Auto Layout tries to satisfy all constraints in order of priority. Priority = 1000 - required. All other, lower priority rules you can set to give clarity to the processing of the location of the elements of your interface. If one of the constraints is not correctly computed, Auto Layout uses the closest constraint and starts building on it. Nevertheless, I strongly recommend not to overload the interface with various rules and use add-ons only to achieve the desired result.

Creating Auto Layout and Its Components


You can create constraint in 3 ways:

1. CTRL + Drag, for example, from label to upper border.
2. Using Stack, Align, Pin and Resolve Tools.
3. Provide an interface builder to build constraints for you.

Among these points, the most important is the 2nd, since the use of this panel is the main tool for creating markup.

Stack is actually the very button with which you can place selected interface details in StackView. Interface Builder decides for itself what StackView will be based on the location of the elements. In addition to the Stack button, StackView can be created by dragging from the object library, like any other item.

Align is a menu that allows you to set elements clearly along a specific line, whether from the side, vertically in the center or below. You often use this approach when aligning text in the center or strictly to the left of the beginning of the page in text editors.

Pin - a menu that allows you to specify a rigid frame relative to its size or the nearest object. In it, you choose which constraint you want to set in one direction or another and set its parameters. Also using this menu, you can, for example, give a group of buttons the same size, which does not change despite the screen scale.

Resolve Tools is the best tool for debugging constraints . The main features of this menu are: remove all rules, add presumptive constraints (Interface Builder will build all the rules for you), add missing constraints , update constraints or frames (position of objects).
As you can see, there are quite a few important points and they are designed to alleviate all the developer’s problems.

You can edit constraint 's by clicking on them in Interface Builder, found in Size Inspector or in the Document Outline list. When editing parameters, you can set identifiers for easier understanding and finding them in the logs and console when performing various debugs.

An important aspect when setting rules for elements is the CHCR (Content-Hugging and Compression-Resistance Priorities) parameters - these parameters affect the change of the element itself depending on the higher view . Roughly speaking, Hugging is an element's unwillingness to increase, and Compression-Resistance is an unwillingness to decrease. Using the CHCR parameters, you can, for example, change the compression-expansion ratio of elements in a StackView depending on the size of the elements in it.

Be careful - macOS and iOS calculate layouts in different ways: At macOS, Auto Layout can change the size of the window and the size of the content, and in iOS it can only change the size of the content, since the system itself determines the size and boundaries of the application.

When writing the article, I was based on the materials of the official guide on Auto Layout

Source: https://habr.com/ru/post/312782/


All Articles