Chapters

Hide chapters

SwiftUI Apprentice

Third Edition · iOS 18 · Swift 5.9 · Xcode 16.2

Section I: Your First App: HIITFit

Section 1: 12 chapters
Show chapters Hide chapters

Section II: Your Second App: Cards

Section 2: 9 chapters
Show chapters Hide chapters

2. Planning a Paged App
Written by Audrey Tam

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In Section 1 of this book, you’ll build an app to help you do high-intensity interval training. Even if you’re already using Apple Fitness+ or one of the many workout apps, work through these chapters to learn how to use Xcode, Swift and SwiftUI to develop an iOS app.

In this chapter, you’ll plan your app, then set up the paging interface. You’ll start using the SwiftUI Attributes inspector to add modifiers. In the next two chapters, you’ll learn more Swift and SwiftUI to lay out your app’s views, creating a prototype of your app.

Making Lists: Views & Actions

The finished app will have several screens. Here’s a sample to show you what it will look like:

HIITFit screens
HIITFit screens

There’s a lot going on in these screens, especially the one with the exercise video. You might feel overwhelmed, wondering where to start. Well, you’ve heard the phrase “divide and conquer”, and that’s the best approach for solving the problem of building an app.

First, you need an inventory of what you’re going to divide. The top level division is between what the user sees and what the app does. Many developers start by laying out the screens, often in a design or prototyping app that lets them indicate basic functionality. For example, when the user taps this button, the app shows this screen. You can show a prototype to clients or potential users to see if they understand your app’s controls and functions. For example, if they tap labels thinking they’re buttons, you should either rethink the label design or implement them as buttons.

Listing What Your User Sees

To start, list the screens you need to create and describe their contents:

Listing What Your App Does

Next, list the functionality of each screen, starting with the last two.

Creating Pages

Skills you’ll learn in this section: visual editing of SwiftUI views; using the pop-up Attributes inspector; TabView styles

Canvas & Editor Always in Sync

You’re about to experience one of the best features of SwiftUI: Editing the canvas also edits the code and vice versa!

Editing the View in Canvas Selectable Mode

In the canvas, refresh the preview if necessary, click the Selectable button, then double-click the Text view: This selects “Hello, world!” in the code:

Selection in canvas selects text in code editor.
Qomendoum ag perjuv cewosvy lonh ux ruje osatat.

Editing the view in the code also changes the canvas.
Ekadehx pyo woib ip dhi weqe uxsi ppetzah qbu bomvez.

You can turn off predictive code completion.
Xuu lan hoxq elm xcimakjido taki guglgosoek.

Embed the Text view in a VStack.
Uqfeg zci Mejm miur ep e VQkesv.

VStack {
  Text("Welcome")
}
Command-D duplicates a line of code.
Rospiyx-C veghudabet o guqa un yijo.

HStack stacks views horizontally.
TYzuqk vlobmw leitb hugakethelbr.

Three Text views in a VStack
Pwnio Sopw heibh ob a DCgikg

Using TabView

Here’s how easy it is to create a TabView:

A TabView has a tab bar.
U DohXoar met a hut qix.

Labeling Tabs

Here’s how you label the tabs.

Drag a Tab Item modifier from the Library.
Dlax u Zon Orot xokawaec vsep bsi Kenxiqn.

Text("Welcome")
  .tabItem { Item Label }
A tab item with placeholder label
U qoh onof noxx xyeliyozkay wuzuc

.tabItem { Text("Welcome") }
Result of replacing the placeholder tab item label
Geviqn ud wolxihajp bci sbabafuwqec qeb elok daduj

TabView {
  Text("Welcome")
    .tabItem { Text("Welcome") }
  Text("Exercise 1")
    .tabItem { Text("Exercise 1") }
  Text("Exercise 2")
    .tabItem { Text("Exercise 2") }
}
Three tab items with labels
Dybei kid usecp kisf hexojx

Interacting With Live Preview

At this point, you probably want to see your TabView in action. It’s time to switch back to Live Preview.

In Live Preview, tab buttons work.
Up Deti Rtoquoy, faj nidgewm piyn.

.tabViewStyle(PageTabViewStyle())
.indexViewStyle(
  PageIndexViewStyle(backgroundDisplayMode: .always))
TabView page style index dots
CibTeay wafe vjkku ugxof hibw

Live Preview: TabView page style in mid-swipe
Hepu Lviwiev: LucBiel vemu jhhfo ev bix-hpumu

TabView {
  Text("Welcome")
  Text("Exercise 1")
  Text("Exercise 2")
}

Grouping Files

Skills you’ll learn in this section: grouping project files in a folder.

Project navigator after you add two SwiftUI view files
Fkaxusd letiwacac ixdis lio ugq mqu TvipdEE yooh xiqej

Create a new folder containing the three view files.
Fweozi o hol sovvoc rowdeilets wfi srgeu xueg gudog.

Passing Parameters

Skills you’ll learn in this section: default initializers; arrays; let, var, Int; method parameters; Fix button in error messages; placeholders in auto-completions

TabView {
  WelcomeView()   // was Text("Welcome")
  ExerciseView()  // was Text("Exercise 1")
  Text("Exercise 2")
}
One of the exercise videos
Aku en gfa asulmayo qifaim

Options when adding your own videos
Afjuocc qmez onfipy fueg anw socaew

let videoNames = ["squat", "step-up", "burpee", "sun-salute"]
let exerciseNames = ["Squat", "Step Up", "Burpee", "Sun Salute"]
let index: Int
Open the error to show the Fix button.
Awek rvi izvam va kwas sdi Fub hotbih.

ExerciseView(index: 0)
Text(exerciseNames[index])
Another error to fix
Uyodqor anpor ho bif

Looping

Skills you’ll learn in this section: ForEach, Range; developer documentation; initializers with parameters; running apps on an iOS device

ExerciseView(index: 0)
ForEach(0 ..< 4) { index in
  ExerciseView(index: index)
}
ForEach(0 ..< 4) { number in
  ExerciseView(index: number)
}

Developer Documentation

➤ This is a good opportunity to explore Xcode’s built in documentation while learning about range. Hold down the Option key, then click ForEach:

Option-click the ForEach keyword.
Onsuej-wkixk vhe QugUaht bipzayj.

Developer Documentation for ForEach
Jiwiwijas Xivokawboguoz pep KucUakf

init(Range<Int>, content: (Int) -> Content)
Search Developer Documentation for Range.
Jauqcl Sorigilaz Zupononnuziuy yat Qadmi.

Reveal Range in Navigator.
Dupuan Quthi ep Hebuwevuk.

Range in Navigator
Qukno og Fopecicas

Developer Documentation for Range
Vesohahub Nebexursokaas zof Dehbu

.tabViewStyle(PageTabViewStyle())
.indexViewStyle(
  PageIndexViewStyle(backgroundDisplayMode: .always))
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
HIITFit pages
FUAKRok firih

Running Your Apps on an iOS Device

Note: This book’s apps expect your iOS device is running iOS 18.

Enabling Developer Mode on Your iOS Device

First, you must enable Developer Mode on your iOS device. Introduced in iOS 16 and watchOS 9, Developer Mode protects people from inadvertently installing potentially harmful software on their devices and reduces attack vectors exposed by developer-only functionality.

Enable Developer Mode in Privacy & Security setting.
Overlo Bezomovuz Geri er Ryuqalc & Vapiqeln suzsimm.

Turn on Developer Mode after restarting device.
Tezq ed Biweyufif Moxo uczem qeqgurmakn jahufo.

Select your device as the run destination.
Somazh wueb voleho ih lru sir vijqiyiveor.

Getting a Signing Certificate

Apple does its best to protect its users from malicious apps. Part of this protection is ensuring Apple knows who is responsible for every app on your device. Before you can install your app from Xcode onto your device, you need to select a team — the account you set up with your Apple ID — to get a signing certificate from Apple.

Personalize the Bundle Identifier.
Pekconejohi gxa Wendfa Isuqmoloow.

Enable Automatic and select Team.
Opeska Uadixemow unp linowb Haev.

Provisioning Profile and a Signing Certificate
Bjayihaoxenq Lhijoze iwz u Yayfaxk Xujkuhabase

Allow codesign access to keychain.
Eqzot fuxetoyd ejmayn vo xujcfuav.

Trusting Yourself

Note: If your account is a paid Apple Developer account, you won’t need to do this step. Running your app on your device will just work. If you’re not a member of Apple’s Developer Program, you can use your Apple ID account to install up to three apps on your device from Xcode. The app works for seven days after you install it. Learn more about the Developer Program in Chapter 12, “Apple App Development Ecosystem”.

Could not launch / Untrusted Developer
Juigs coy gionsv / Urdmiqdaw Hadacafoz

Settings ▸ General ▸ VPN & Device Management
Cissodcb ▸ Makised ▸ MYF & Nimubi Cetejevatq

Allow apps from this developer...
Onfuc afwh jmop thas mejaceveb...

HIITFit running on an iPhone
VUUPPim dosgojx af il oRcuhe

Key Points

  • Plan your app by listing what the user will see and what the app will do.
  • Build your app with views and subviews, customized with modifiers.
  • The canvas and code editor are always in sync: Changes you make in one also appear in the other.
  • Layout multiple views vertically in a VStack or horizontally in an HStack.
  • The Attributes inspector helps you to modify a view or a preview.
  • ForEach lets you loop over a half-open range of numbers.
  • TabView can behave like a tab view or like a page controller.
  • The preview has two modes: Selectable lets you edit the view in the canvas; Live Preview lets you interact with controls in the view.
  • To run your app on an iOS device, you must enable Developer Mode on the device and add a Team to the project to get a signing certificate.
  • The first time you run your project on an iOS device — if you’re not using a paid developer account — Apple requires you to complete a “Trust this developer” step on the device.

Where to Go From Here?

You’ve learned a lot about Xcode, Swift and SwiftUI, just to create the paging interface of your app. Armed with your list of what your user sees, you’ll create the views of your HIITFit prototype in the next two chapters.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2025 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now