At the top level, your new interface will have a vertical stack view with four elements displaying the item’s name, serial number, value, and date created (Figure 11.2).

Open your LootLogger project and then open Main.storyboard. Drag a new View Controller from the library onto the canvas. Drag a Vertical Stack View from the library onto the view for the View Controller. Add four constraints to the stack view: Pin it to the leading and trailing margins, then pin the top and bottom edges 8 points from the top and bottom of the safe area.

Now drag four instances of UILabel from the library onto the stack view. From top to bottom, give these labels the text Name, Serial, Value, and Date Created (Figure 11.3).

You might notice a warning that some views are vertically ambiguous. And if you select any of the labels, you will see that they all have a red top and bottom border (indicating a vertical Auto Layout problem). There are two ways you can fix this issue: by using Auto Layout or by using a property on the stack view. Let’s work through the Auto Layout solution first, because it highlights an important aspect of Auto Layout.

You learned in Chapter 3 that every view has an intrinsic content size. You also learned that if you do not specify constraints that explicitly determine the width or height, the view will derive its width or height from its intrinsic content size. How does this work?

It does this using implicit constraints derived from its content hugging priorities and its content compression resistance priorities. A view has one of each of these priorities for each axis:

  • horizontal content hugging priority

  • vertical content hugging priority

  • horizontal content compression resistance priority

  • vertical content compression resistance priority

The label and text field look a little squished because there is no spacing between them. This is easy to fix, because stack views allow you to customize the spacing between items.

Select the horizontal stack view and open its attributes inspector. Change the Spacing to be 8 points. Notice that the text field shrinks to accommodate the spacing, because it is less resistant to compression than the label.

Repeat these steps for the Serial and Value labels:

  1. Select the label, click the Stack view spacing icon, and select Stack View.

  2. Change the stack view to be a horizontal stack view.

  3. Drag a text field onto the horizontal stack view and change its horizontal content compression resistance priority to be 749.

  4. Update the stack view to have a spacing of 8 points.

There are a couple of other tweaks you will want to make to the interface: The vertical stack view needs some spacing. The Date Created label should have a center text alignment. And the Name, Serial, and Value labels should be the same width.

Select the vertical stack view, open its attributes inspector, and update the Spacing to be 8 points. Then select the Date Created label, open its attributes inspector, and change the Alignment to be centered. That solves the first two issues.

Although stack views substantially reduce the number of constraints that you need to add to your interface, some constraints are still important. With the interface as is, the text fields do not align on their leading edge due to the difference in the widths of the labels. (The difference is not very noticeable in English, but it becomes more pronounced when localized into other languages.) To solve this, you will add leading edge constraints between the three text fields.

Control-drag from the Name text field to the Serial text field and select Leading. Then do the same for the Serial text field and the Value text field. The completed interface will look like Figure 11.8.

Stack views allow you to create very rich interfaces in a fraction of the time it would take to configure them manually using constraints. Constraints are still added, but they are being managed by the stack view itself instead of by you. Stack views allow you to have very dynamic interfaces at runtime. You can add and remove views from stack views by using addArrangedSubview(_:), insertArrangedSubview(_:at:), and removeArrangedSubview(_:). You can also toggle the hidden property on a view in a stack view. The stack view will automatically lay out its content to reflect that value.