4 ms·
The divide trick is interesting; I've used it here and there over the years but never came to rely on it. Looks like the pattern became much more concise with S
by gwking 9y ago
The divide trick is interesting; I've used it here and there over the years but never came to rely on it. Looks like the pattern became much more concise with Swift's tuple unpacking.
For what it's worth, I settled on "two uncool tricks": the venerable autoresize mask, which I have aliased to be shorter words, and extension properties for top, left, right, bottom, and center. These are just convenience accessors over a view (or layer's) frame, but there is one crucial design choice: when right or bottom mutates, it moves the origin (as opposed to resizing - this way left/right/top/bottom all have similar meaning).
Together these properties lets me write code that states intent more directly and succinctly, e.g. for a content view and a bottom bar, `bar.bottom = view.height; content.height = bar.top;`. It's still imperative code, and order matters, but it reads decently well. Conditional insertions can be just that, etc. Lastly, it helps immensely to factor out constants into named properties, e.g. `let y_margin = 4; let text_pad = 2`, especially for maintenance.
It is essentially the x-y cursor approach without a cursor - just use the relative properties of the layout built thus far to position the next item.
As for autoresize, the dumb trick worth mentioning is to initialize autoresized root views with a weird size, e.g. 99x99. Then if something is not configured right you will see the telltale square.
I have given auto layout several serious tries, most recently in 2014 or so. I tried both Interface Builder (which I deeply dislike due to nonsense XML git conflicts) and code. I found that with a few extension properties I could write auto layout code by hand effectively, but what ruined it for me was that ApKit throws layout exceptions. In general I appreciate Apple's approach to exception handling but to have my view crash before I could even see the layout was very unproductive. In contrast, when I make an autoresize mistake I can usually see what the problem is and correct it, rather than guessing at which combination of constraints caused an exception.
I would be curious to hear from folks if the auto layout situation has improved, and if there are tools that have helped with debugging (I used to rely on Reveal a lot).