00:06 Today we'll revisit the code from the last episode, and without adding any features, we're going to improve the layout code. We generally use the same pattern for every view: we add the subview, set translatesAutoresizingMaskIntoConstraints to false, and add a bunch of constraints. Let's see what happens when we combine these actions in a single method.
addSubview with Constraints
00:57 We already use our custom addSubview method of Box, so it's easy to extend this method by taking a constraints array:
02:26 There's still a lot of repetition: we almost always use the new subview on the left-hand side of the constraint and the superview to which we append on the right. We often also use the same anchors for both views — the child's left anchor is constrained to the parent's left anchor, the middle anchor to the middle, etc.
Simplifying Common Constraints
03:03 We create the type alias Constraint for a function that takes a child view and a parent view and returns a layout constraint between the two:
06:11 We could keep writing functions that combine various anchors, but Swift's key paths can help us write a more flexible function that creates constraint combinators for us. By giving the function two key paths from a UIView to an NSLayoutAnchor, it can return a Constraint combinator:
11:51 This compiles and works, and we can update a lot of our code to work the same way. To update the track info view's layout, we need another helper method to constrain an anchor to a constant. Constraining to a constant only works for NSLayoutDimension, which is a subclass of NSLayoutAnchor:
14:12 This is a lot more declarative than what we had before. And we can use our helper methods to create even more complex constraints, like the button that is constrained to the map view's safeAreaLayoutGuide:
16:54 It's possible to use a combination of techniques; we don't have to use the same declarative way of creating constraints everywhere — for example, our track info view uses another constraint that we added manually.
17:09 Our layout code has been cleaned up quite a bit! Let's see if we can improve the app further next week.