Follow along at https://www.hackingwithswift.com/100/36.
This day covers the first part of Project 8: 7 Swifty Words
in Hacking with Swift.
I have a separate repository where I've been creating projects alongside the material in the book. And you can find Project 8 here. However, I also copied it over to this day's folder so I can extend from where I left off.
With that in mind, Day 36 focuses on a particular topic: building a UIKit user interface programmatically.
Our interface has the following items, stacked vertically:
- A score label
- Two labels for a list of clues and their corresponding answers
- The user's current answer
- Submit and Clear buttons
- A grid of letter groups that the user can choose from to assemble their answer.
To me, this arrangement demonstrates why storyboards and code can often be interwoven — and why knowing when to reach for either one can be a bit of an art, but an invaluable skill.
For one, given all of the anchoring constraints we're setting relative to other elements, it seems like doing everything in the storyboard might not even be possible. But while we could do everything in code, I don't think we should give up on the storyboard completely.
I settled on using the storyboard to mock up a basic skeleton of the UI — adding the labels, the submit/clear buttons, the answer text, and a container for the grid of letter groups — and then dug in to create anchors and letter-group buttons in code.
Regardless of whether or not I could have moved more — or everything — to code, the ability, in particular, to have our code set layout constraints relative to other elements's anchors is some raw (and beautifully explicit) power that's hard to beat 💪.