Why Auto Layout?

Why Auto Layout instead of the horizontal and vertical boxes used by other systems like Qt? Or a layout system like CSS?


What is missing in the other ways of doing this stuff?


Over a couple years, Auto Layout has given me mostly headaches, and I now generally try to avoid it, so I want to know if there's a good reason for my suffering.


thanks!

Replies

I'd speculate that this the wrong place to expect to find someone in the mood to reliably defend al.


Otherwise, your references don't seem accurate - seems pointless to compare open web based standards with a closed mobile ecosystem. See the HIGs, for starters on why.

I guess because a general-purpose system like AL can serve as the basis for a hierarchical box model, but not the other way around. It could definitely use more higher-level constructs like stackviews, and the tooling could be improved, but it seems pretty straightforward why they built the underlying layout engine that way.

Localization - there is no better way to do it than auto layout


Certainly, autolayout isn't easy. It is much better now than in previous versions. Currently, at least for macOS apps, there are no autolayout contraints defined by default. In earlier versions, it was more aggressive about defining constraings for you. All too often, the auto-generated constraints will be wrong.


If you are careful, and make sure to add only the absolute minimum number of constraints, you can make it work. Sometimes, with things like multi-line text fields, you still have to hack it with code.


If you don't need much backwards compatibility, there are some new layout options that let you live almost entirely in a box model if you want.

Living in a box model sounds great! What layout options are you referring to?

A good reason to suffer ? To avoid suffering much more without AutoLayout !


Sure, it can still be improved, notably on diagn ostic and proposed solutions to resolve issues. But one can also use "Set constraints automatically" and XCode will do most of the work.


In some rare cases, I find it better to build the constraints manually, but still use Autolayout.

Stackviews with a light sprinkling of extra constraints is enough for a lot of layouts.

And grid view

The problem with the Apple Layout tools is the poor quality of the documentation. In the past, I have found looking through the Javadoc documentation on their auto layout tools and tried mapping it back to the Apple documentation. Actually, there was a video on "container views" from Apple a few years ago https://developer.apple.com/videos/play/wwdc2017/218/ which was interesting but probably helped me more than it would others due to my Java background.

Using a graphic tool like Interface Builder can make it difficult to understand what the code is doing since you don't know what actions in IB are really doing to the code.

Automatic layout tools like the container views can also make it much easier to reuse and modify code. This is going to be more of an issue as Apple moves to trying to have common code bases on iOS and MacOS. There are a lot of tricks that you can do with the automatic layout tools allowing users to customize their views.

A big advantage is to write your own views that can encapsulate view within other views. A example would be adding scrolling to other views.

The advantages become much more apparent when you have a lot of experience in coding large projects (over 100,000 lines of efficiently written code).