Thursday, August 6, 2020

Emulating Equal-Size Constraints in SwiftUI

Canis:

But the layout engine has a significant limitation: unlike Auto Layout, it’s strictly one-way. Superviews tell subviews how much space they have available, not the other way around. This makes many common tasks fairly straightforward, but it lacks some of the features of Auto Layout — most notably, equal-size constraints.

These are useful where you want some controls to be the same width, height, or both, but you don’t know in advance what that size will be — typically because it’s based on the size of a text label, and you want to support accessibility and localisation, which means different size fonts and different length labels.

[…]

Everyone focuses on the declarative part, because that one word leaked out into the media before it was officially announced, and so people latched on to it. […] But the other parts are important too, and here we see the composability part in action: even though there’s a gap in the layout engine, and it’s quite a bit of hassle to fill, we can take that mess, and turn it into an easily-reusable component or set of components.

This seems like something that should be easy, though.

Previously:

Comments RSS · Twitter

Leave a Comment