4.
Prototyping Supplementary Views
Written by Audrey Tam
Your app still needs three more full-screen views:
- Welcome
- History
- Success
In the previous chapter, you laid out the Exercise view. In this chapter, you’ll lay out the History and Welcome views then complete the challenge to create the Success view. And your app’s prototype will be complete.
Creating the History view
Skills you’ll learn in this section: working with dates; extending a type; Quick Help comments; creating forms; looping over a collection; layering views with
ZStack
; stack alignment values
In this chapter, you’ll just do a mock-up of the list view. After you create the data model in the next chapter, you’ll modify this view to use that data.
➤ Continue with your project from the previous chapter or open the project in this chapter’s starter folder.
➤ Create a new SwiftUI View file named HistoryView.swift. For this mock-up, add some sample history data to HistoryView
, above body
:
let today = Date()
let yesterday = Date().addingTimeInterval(-86400)
let exercises1 = ["Squat", "Step Up", "Burpee", "Sun Salute"]
let exercises2 = ["Squat", "Step Up", "Burpee"]
You’ll display exercises completed over two days.
➤ Replace Text("Hello, World!")
with this code:
VStack {
Text("History")
.font(.title)
.padding()
// Exercise history
}
You’ve created the title for this view with some padding around it.
Creating a form
SwiftUI has a container view that automatically formats its contents to look organized.
➤ Inside the VStack
, replace // Exercise history
with this code:
Form {
Section(
header:
Text(today.formatted(as: "MMM d"))
.font(.headline)) {
// Section content
}
Section(
header:
Text(yesterday.formatted(as: "MMM d"))
.font(.headline)) {
// Section content
}
}
Inside the Form
container view, you create two sections. Each Section
has a header with the date, using headline
font size.
This code takes yesterday and today’s date as the section headers, so your view will have different dates from the one below:
Extending the Date type
When you created the timer view, you had a quick look at the Swift Date
type and used one of its methods. It’s now time to learn a little more about it.
Swift Tip: A
Date
object is just some number of seconds relative to January 1, 2001 00:00:00 UTC. To display it as a calendar date in a particular time zone, you must use aDateFormatter
. This class has a few built-in styles namedshort
,medium
,long
andfull
, described in links from the developer documentation page forDateFormatter.Style
. You can also specify your own format as aString
.
➤ Open DateExtension.swift. The first method shows how to use a DateFormatter
.
func formatted(as format: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: self)
}
DateFormatter
has only the default empty initializer. You create one, then configure it by setting the properties you care about. This method uses its format
argument to set the dateFormat
property.
In HistoryView
, you pass "MMM d"
as format
. This specifies three characters for the month — so you get SEP or OCT — and one character for the day — so you get a number. If the number is a single digit, that’s what you see. If you specify "MM dd"
, you get numbers for both month and day, with leading 0 if the number is single digit: 09 02 instead of SEP 2.
Once you’ve configured dateFormatter
, its string(from:)
method returns the date string.
You don’t have to worry about time zones if you simply want the user’s current time zone. That’s the default setting.
Formatting Quick Help comments
Extending the Date
class with formatted(as:)
makes it easy to get a Date
in the format you want: today.formatted(as: "MMM d")
.
Swift Tip: You can add methods to extend any type, including those built into the software development kit, like
Image
andDate
. Then, you can use them the same way you use the built-in methods.
➤ Look at the comment above the formatted(as:)
method:
/// Format a date using the specified format.
/// - parameters:
/// - format: A date pattern string like "MM dd".
This is a special kind of comment. It appears in Xcode’s Quick Help when you Option-click the method name:
It looks just like all the built-in method summaries!
It’s good practice to document all the methods you write this way. Apple’s documentation for Formatting Quick Help is at apple.co/33hohbk.
Looping over a collection
➤ Now, head back to HistoryView.swift to fill in the section content.
To display the completed exercises for each day, you’ll use ForEach
to loop over the elements of the exercises1
and exercises2
arrays.
➤ In the first Section
, replace // Section content
with this code:
ForEach(exercises1, id: \.self) { exercise in
Text(exercise)
}
In ContentView
, you looped over a number range. Here, you’re using the third ForEach
initializer:
init(Data, id: KeyPath<Data.Element, ID>, content: (Data.Element) -> Content)
The exercises1
array is the Data
and \.self
is the key path to each array element’s identifier. The \.self
key path just says each element of the array is its own unique identifier.
As the loop visits each array element, you assign it to the local variable exercise
, which you display in a Text
view.
➤ In the second Section
, replace // Section content
with the almost identical code:
ForEach(exercises2, id: \.self) { exercise in
Text(exercise)
}
This time, you display the exercises2
array.
➤ Refresh the preview to admire your exercise history:
Creating a button in another layer
In Chapter 6, “Adding Functionality to Your App”, you’ll implement this view to appear as a modal sheet, so it needs a button to dismiss it. You’ll often see a dismiss button in the upper right corner of a modal sheet. The easiest way to place it there, without disturbing the layout of the rest of HistoryView
, is to put it in its own layer.
If you think of an HStack
as arranging its contents along the device’s x-axis and a VStack arranging views along the y-axis, then the ZStack
container view stacks its contents along the z-axis, perpendicular to the device screen. Think of it as a depth stack, displaying views in layers.
➤ Command-click VStack
to embed it in a ZStack
, then add this code at the top of ZStack
:
Button(action: {}) {
Image(systemName: "xmark.circle")
}
Here’s the top part of your view now:
The button is centered in the view, because the default stack alignment is center
. Because you added the Button
code above the VStack
in the source code, it’s underneath the VStack
on screen, so you see only its outline.
The arrangement is a little counter-intuitive unless you think of it as placing the first view down on a flat surface, then layering the next view on top of that, and so on. So declaring the button as the first view places it on the bottom of the stack. If you want the button in the top layer, declare it last in the ZStack
.
It doesn’t matter in this case, because you’ll move the button into the top right corner of the view, where there’s nothing in the VStack
to cover it.
You can specify an alignment
value for any kind of stack, but they use different alignment
values. VStack
alignment
values are horizontal: leading
, center
or trailing
. HStack
alignment
values are vertical: top
, center
, bottom
, firstTextBaseline
or lastTextBaseline
.
To specify the alignment of a ZStack
, you must set both horizontal and vertical alignment values. You can either specify separate horizontal and vertical values, or a combined value like topTrailing
.
➤ Replace ZStack {
with this:
ZStack(alignment: .topTrailing) {
You set the ZStack
alignment
parameter to position the button in the top right corner of the view. Other views in the ZStack
have their own alignment
values, so the ZStack
alignment
value doesn’t affect them.
The button is now visible, but it’s small and a little too close to the corner edges.
➤ Add these modifiers to the Button
to adjust its size and position:
.font(.title)
.padding(.trailing)
Refresh the preview to see the result:
Creating the Welcome view
Skills you’ll learn in this section: refactoring/renaming a parameter; modifying images; using a custom modifier;
Button
label with text and image
➤ Open WelcomeView.swift.
WelcomeView
is the first page in your app’s page-style TabView
, so it should have the same header as ExerciseView
.
➤ Replace Text("Hello, World!")
with this line:
HeaderView(exerciseName: "Welcome")
You want the title of this page to be “Welcome”, so you pass this as the value of the exerciseName
parameter. HeaderView
also displays the page numbers of the four exercises:
Refactoring HeaderView
Using HeaderView
here raises two issues:
- There’s no page number for the Welcome page.
- The parameter name
exerciseName
isn’t a good description of “Welcome”.
The first issue is easy to resolve. The app has only one non-exercise page, so you just need to add another page ”number” in HeaderView
.
➤ In HeaderView.swift, in the canvas preview, duplicate the 1.circle
Image
, then change the first Image
to display a hand wave:
Image(systemName: "hand.wave")
➤ Refresh the preview to see how it looks:
That’ll do nicely.
Now to rename the exerciseName
property. Its purpose is really to be the title of the page, so titleText
is a better name for it.
You could search for all occurrences of exerciseName
in your app, then decide for each whether to change it to titleText
. In a more complex app, this approach almost guarantees you’ll forget one or change one that shouldn’t change.
Xcode has a safer way!
➤ Command-click the first occurrence of exerciseName
and select Rename… from the menu:
Note: If you Command-click
exerciseName
inText(exerciseName)
, you’ll see the longer menu that includes Embed in HStack etc. Rename… is at the bottom of this menu.
Xcode displays all the code statements that need to change:
➤ The first instance is highlighted differently. Type titleText, and all the instances change:
➤ Click the Rename button in the upper right corner to confirm these changes, then head back to WelcomeView.swift to see the results:
That’s better! The user sees a page icon, and the programmer sees a descriptive parameter.
More layering with ZStack
So far, so good, but the header should be at the top of the page. There’s also a History button that should be at the bottom of the page. The main content should be centered in the view, independent of the heights of the header and button.
In HistoryView
, you used a ZStack
to position the dismiss button in the upper right corner (topTrailing
), without affecting the layout of the other content.
In this view, you’ll use a ZStack
to put the header and History button in one layer, to push them apart. Then you’ll create the main content in another layer, centered by default.
➤ First, embed HeaderView
in a VStack
, then embed that VStack
in a ZStack
.
ZStack {
VStack {
HeaderView(titleText: "Welcome")
}
}
➤ In the VStack
, below HeaderView
, add this code:
Spacer()
Button("History") { }
.padding(.bottom)
You have the header and the History button in a VStack
, with a Spacer
to push them apart and some padding so the button isn’t too close to the bottom edge:
➤ Now to fill in the middle space. Add this layer to the ZStack
:
VStack {
HStack {
VStack(alignment: .leading) {
Text("Get fit")
.font(.largeTitle)
Text("with high intensity interval training")
.font(.headline)
}
}
}
Note: You can add this
VStack
either above or below the existingVStack
. It doesn’t matter because there’s no overlapping content in the two layers.
The inner VStack
contains two Text
views with different font sizes. You set its alignment to leading
to left-justify the two Text
views.
This VStack
is in an HStack
because you’re going to place an Image
to the right of the text. And the HStack
is in an outer VStack
because you’ll add a Button
below the text and image.
Modifying an Image
➤ Look in Assets.xcassets for the step-up image:
➤ Back in WelcomeView.swift, open the Library with Shift-Command-L (or click the + toolbar button) and select the media tab:
➤ To insert step-up in the correct place, it’s easiest to drag it into the code editor. Hold onto it while nudging the code with the cursor, until a line opens, just below the VStack
with two Text
views. Let go of the image, and it appears in your code:
HStack {
VStack(alignment: .leading) {
Text("Get fit")
.font(.largeTitle)
Text("with high intensity interval training")
.font(.headline)
}
Image("step-up") // your new code appears here
}
➤ You usually have to add several modifiers to an Image
, so open the Attributes inspector in the inspectors panel:
Note: If you don’t see Image with a value of step-up, select the image. You might have to close then reopen the inspector panel.
➤ First, you must add a modifier that lets you resize the image. In the Add Modifier field, type resiz then select Resizable.
Don’t worry if the image stretches. You’ll fix that with the next modifier.
➤ When resizing an image, you usually want to preserve the aspect ratio. So search for an aspect modifier and select Aspect Ratio:
➤ The suggested contentMode
value is fill
, which is what you usually want, so press Return to accept it.
➤ Now the image looks more normal, but it’s too big. In the Frame section, set the Width and Height to 240:
That’s looking pretty good! How about clipping it to a circle?
➤ Search for a clip modifier and select Clip Shape:
➤ Again, the suggestion Circle()
is what you want, so accept it.
Your HStack
code and canvas now look like this:
➤ You need just one more tweak: The text would look better if you align it with the bottom of the image. Just change the alignment of the enclosing HStack
:
HStack(alignment: .bottom)
And here’s your Welcome page:
You’ve done enough to make it look welcoming. :] In Chapter 10, “Refining Your App”, you’ll add a few more images.
Using a custom modifier
You’ll use this triplet of Image
modifiers all the time:
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 240.0, height: 240.0)
Everyone does, although the frame dimensions won’t always be 240. In ImageExtension.swift, you’ll find resizedToFill(width:height:)
which encapsulates these three modifiers:
func resizedToFill(width: CGFloat, height: CGFloat)
-> some View {
return self
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: width, height: height)
}
It extends the Image
view, so self
is the Image
you’re modifying with resizedToFill(width:height:)
.
➤ To use this custom modifier, head back to WelcomeView.swift. Comment out (Command-/) or delete the first three modifiers of Image("step-up")
, then add this custom modifier:
.resizedToFill(width: 240, height: 240)
And the view looks the same, but there’s a little less code.
Labeling a Button with text and image
The final detail is a Button
. The user can tap this to move to the first exercise page, but the label also has an arrow image to indicate they can swipe to the next page.
The other buttons you’ve created have only text labels. But it’s easy to label a Button
with text and an image.
➤ In the center view VStack
, below the HStack
with the image, add this code:
Button(action: { }) {
Text("Get Started")
Image(systemName: "arrow.right.circle")
}
.font(.title2)
.padding()
This code is quite different from the other buttons you’ve created and requires some explanation. SwiftUI uses a lot of syntactic sugar: Instead of using the official method calls, SwiftUI lets you write code that’s much simpler and more readable.
If you start typing Button in the code editor, Xcode will auto-suggest this official Button
signature:
Button(action: {}, label: {
<Content>
})
Button
has two parameters: action
is a method or a closure containing executable code; label
is a view describing the button’s action
. Both parameter values can be closures, so action
can be more than one executable statement, and label
can be more than one view.
Swift Tip: You can move the last closure argument of a function call outside the parentheses into a trailing closure.
If you drag a Button
into your view from the canvas library, you get this version of the Button
signature with the label
content as a trailing closure:
Button(action: {} ) {
<Content>
}
This is the syntax used in the “Get Started” Button
above, with the Text
and Image
views in an implicit HStack
.
The other buttons you’ve created use an even simpler syntax for Button
, where the button’s label is just a String
, and the button’s action is in the trailing closure. For example:
Button("History") { }
➤ The Label
view is another way to label a Button
with text and image. Comment out (Command-/) the Text
and Image
lines, then write this line in the label
closure:
Label("Get Started", systemImage: "arrow.right.circle")
Look closely: Do you see what changed?
Note: You can modify a
Label
withlabelStyle
to show only the text or only the image.
The image is on the left side of the text. This looks wrong to me: An arrow pointing right should appear after the text. Unfortunately for this particular Button
, there’s no way to make the image appear to the right of the text, unless you’re using a language like Arabic that’s written right-to-left. Label
is ideal for icon-text lists, where you want the icons nicely aligned on the leading edge.
➤ Delete the Label
and uncomment the Text
and Image
.
➤ Just for fun, give this button a border. Add this modifier below padding()
:
.background(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.gray, lineWidth: 2))
You put a rounded rectangle around the padded button, specifying the corner radius, line color and line width.
Challenge
When your users tap Done on the last exercise page, your app will show a modal sheet to congratulate them on their success.
Your challenge is to create this SuccessView
:
Challenge: Creating the Success view
- Create a new SwiftUI View file named SuccessView.swift.
- Replace its
Text
view with aVStack
containing thehand.raised.fill
SF Symbol and the text in the screenshot. - The SF Symbol is in a 75 by 75 frame and colored purple. Hint: Use the custom
Image
modifier. - For the large “High Five!” title, you can use the
fontWeight
modifier to emphasize it more. - For the three small lines of text, you could use three
Text
views. Or refer to our Swift Style Guide bit.ly/30cHeeL to see how to create a multi-line string.Text
has amultilineTextAlignment
modifier. This text is colored gray. - Like
HistoryView
,SuccessView
needs a button to dismiss it. Center a Continue button at the bottom of the screen. Hint: Use aZStack
so the “High Five!” view remains vertically centered.
Here’s a close-up of the “High Five!” view:
You’ll find the solution to this challenge in the challenge folder for this chapter.
Key points
- The
Date
type has many built-in properties and methods. You need to configure aDateFormatter
to create meaningful text to show your users. - Use the
Form
container view to quickly lay out table data. -
ForEach
lets you loop over the items in a collection. -
ZStack
is useful for keeping views in one layer centered while pushing views in another layer to the edges. - You can specify vertical alignment values for
HStack
, horizontal alignment values forVStack
and combination alignment values forZStack
. - Xcode helps you to refactor the name of a parameter quickly and safely.
-
Image
often needs the same three modifiers. You can create a custom modifier so you Don’t Repeat Yourself. - A
Button
has a label and an action. You can define aButton
a few different ways.
Where to go from here?
Your views are all laid out. You’re eager to implement all the button actions. But … you’ve been using hard-coded sample data to lay out your views. Before you can make everything work, you need to design your data model. The Model-View-Controller division of labor still applies. And you’ll learn lots more about Swift and Xcode.