Apple's auto layout and visual format language for JavaScript (2016)
ijzerenhein.github.io
ijzerenhein.github.io
This model can't express wrapping, so it can't gracefully scale layouts down from desktop to mobile sizes (when you have a 3-column layout you have lots of control over column widths, but no way of turning them into 3 rows instead).
CSS Grid and Flexbox are pretty good. Conceptually they're messier than mathematically-beautiful constraints, but in practice they're more powerful and more robust.
On Apple platforms (UIKit on iOS) handling such layout changes is done with another mechanism on top of auto layout. Something they call "Size Classes". You get a callback saying eg. "view has changed to Regular width, Compact height" and you switch to a another set of auto-layout constraints + animate as desired. This size class system is used for stuff like handling rotation on the iPhone and transitioning to split-screen mode on iPad.
Also anything with scroll views and/or stack views has been a nightmare in my experience. What should happen is that the stack view and scroll view should “expand” outward from their sub views based on those sub views intrinsic content sizes. Instead a lot of the time autolayout seems to try to start with the scroll view or stackviews size and compress “inwards” and so the only way Autolayout thinks it can satisfy everything is for everything to have a size of zero and your views disappear.
Also mobile devices don’t seem to conform well with rigid grid systems from a design standpoint. System provided UI conponents(like tab bars and nav bars on iOS) have their own sizes that won’t conform to your grid, users can change text size (a good thing), and animations and “physics” (like UIKitDynamics) necessarily mean you won’t have a grid.
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0)
]) NSLayoutConstraint.activate([
button.centerXAnchor ≈ view.centerXAnchor,
button.centerYAnchor ≈ view.centerYAnchor + 10
])For context, I've had the options on to warn on expensive expressions for a long time and we use a lot of these operators everywhere and I think only once did we ever have an autolayout line flagged, and that was due to a complicated expression used to come up with the constant value.
Especially since Apple admitted their mistake and is moving towards SwiftUI.
This is not Auto layout, it is just an alternative string-based API to express constraints in Auto layout. In practice no-one (that I know) uses Visual Format Language, as it is called, but instead sets up constraints in code or in Xcode Interface builder. In Swift code it would look something like this: view1.leadingAnchor.constraint(equalTo: view2.leadingAnchor, constant: 10) view2.topAnchor.constraint(equalTo: view1.bottomAnchor) ...
Maybe CSS is syntactically easier, but the relationships between elements are much more hidden (width: 100%; of what?) so I'd call it a wash.
[0] https://css-tricks.com/snippets/css/complete-guide-grid/
P.S. I know there are a bunch of frameworks out there that do the same thing I built. The company I built it for specifically wanted a custom one for themselves. It's decisions like that that led me to leave. Still, fun project and I learned a ton.
It seems this was Apple’s auto layout, but links to the developer documentation are now broken. My conclusion the last time I looked into this is that it seems Apple has switched to a different approach with their new SwiftUI layout engine.
Edit: this blog post from October discusses the switch from Auto Layout to SwiftUI in some detail https://kean.github.io/post/swiftui-layout-system
Having worked with a lot of different layout and alignment systems, Auto Layout seemed to me to be considerable overkill. I found it generally not worth all the brain power needed to understand it and more importantly debug it when it isn't doing what you want.
The whole time I was doing it, I wondered why I couldn't just use CSS.
It seems that every UI toolkit has a different method. And every one that I've worked with makes me wonder why they don't just use CSS.