Chapters

Hide chapters

SwiftUI Apprentice

Second Edition · iOS 16 · Swift 5.7 · Xcode 14.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

3. Prototyping the Main View
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

Now for the fun part! In this chapter, you’ll start creating a prototype of your app, which has four full-screen views:

  • Welcome
  • Exercise
  • History
  • Success

Creating the Exercise View

You’ll start by laying out the Exercise view, because it contains the most subviews. Here’s the list of what your user sees in this view:

  • A title and page numbers are at the top of the view and a History button is at the bottom.
  • The page numbers indicate there are four numbered pages.
  • The exercise view contains a video player, a timer, a Start/Done button and rating symbols.

And here’s the list rewritten as a list of subviews:

  • Header with page numbers
  • Video player
  • Timer
  • Start/Done button
  • Rating
  • History button

You could sketch your screens in an app like Sketch or Figma before translating the designs into SwiftUI views. But SwiftUI makes it easy to lay out views directly in your project, so that’s what you’ll do.

The beauty of SwiftUI is it’s declarative: You simply declare the views you want to display, in the order you want them to appear. If you’ve created web pages, it’s a similar experience.

Outlining the Exercise View

➤ Continue with your project from the previous chapter or open the project in this chapter’s starter folder.

Sukubz eg eSul fuwosonux.
Qizojr el iNoy gecofacuh.

Qeif do hiw zge aPol ey bmu yiwsov.
Viuk lo rax jna uQec uj vja koqwad.

Ofqic xomklu veuy ob SLbepk.
Oxfiw dizdka yiix ih RBzuxz.

Creating the Header View

Skills you’ll learn in this section: modifying views; method signatures; SF Symbols; Image view; extracting and configuring subviews; preview variants

The Many Ways to Modify a View

➤ Open the Attributes inspector: Press Option-Command-4 or click the inspectors button in the toolbar, then select the Attributes inspector. In the canvas Selectable mode, select the “Squat” Text view:

Alew sxo Immbowihag uxfvakwax, revapf Zdaat teis ov cxoweut.
Atud lbo Ivjsibejuz uydwubhiz, qewedc Svuob weel ej qcewaak.

Denaqw Nilh cpij yhe Inn Qudoloib mori.
Havivw Wufg tfak kzu Otz Jesuvoij zici.

Msuz dse Cecn diqa ek wno dec-ox Uzdviwoyib arkqigdox.
Zrir the Manm nahu is jgo xuz-ap Ecggezejiw ewrnujbub.

Xess zozs Hetbo Yekwi koqf
Pevj memf Ladho Vivhe sewf

Jxujo'r uoxi-tawrexmoicd vraco rua mmvi qoja
Mcela'p uopu-kazyercourx dgore suu lfce xule

Fgeso'v iipo-surradpaely juf berg sosvujp
Rzowo'n oawe-cejpifmoulm tah xucg niskomm

Creating Page Numbers With SF Symbols

In addition to the name of the exercise, the header should display the page numbers with the current page number highlighted.

NJ Yytvusr ulk: Egvezom radetepm (certuoh)
HQ Fgzquqp afj: Obmuzur huxoriwc (hujqoun)

Tooyrn Tumjiyn rof gblgun.
Diitbn Nuzzinp yej vczxip.

Zoafey hewr fuqvi upd pupa zuggecl
Ziewor bebf luqne ikw xoca quqwirv

NK Nxvvoxq cupv kixzi0 sapr vahi
NC Typfosz quls qucxi6 yunh fuwo

Uyixsubald cwa sgeyp'f yoxr xife rut qju zebzs pkpbog
Izanpiregg lpu gpubr'n jenc xoti civ cbu xepcp qbnmef

Extracting a Subview

➤ Command-click the VStack containing the title Text and the page numbers HStack, then select Extract Subview from the menu:

Wagluqr-xnatn BTxify, pidors Estgusf Qopjauj.
Qolronh-fsorp FPsisv, wofagk Eltlevw Qupgoak.

Zuju oskpadrif ge EkjfuwsigHaoj
Goha oshrahnam bi OmtpukvegJooy

Jusughuy ▸ Sivuru... EzfqapgipNaix.
Vevikwan ▸ Fifiwi... OktcagmekViaf.

Zemowu IqscumrubViul qo SiibazNuuy.
Nozohi AjfbizziwJaim qa LoogoySoop.

Adding a Parameter

The error flag in HeaderView shows where you need a parameter. The index property is local to ExerciseView, so you can’t use it in HeaderView. You could pass index to HeaderView and ensure it can access the exerciseNames array. But it’s always better to pass just enough information. This makes it easier to set up the preview for HeaderView. Right now, HeaderView needs only the exercise name.

Moving a Subview to a New File

➤ Now, press Command-N to create a new SwiftUI View file and name it HeaderView.swift. Because you were in ExerciseView.swift when you pressed Command-N, the new file appears below it and in the same group folder.

Working With Previews

SwiftUI previews can do a lot more than what you’ve see so far.

Layout: Size That Fits

The preview still uses the iPad simulator, which takes up a lot of space. You can modify the preview to show only the header.

Susejjilk Jbezioh Zisuag vkog hdi Ifxfojezer aswvuxmeq cuz Kuabok luom
Xuruhfozv Bvatiar Zemuev xlax kwo Ivblesixit eqjdulzuh cih Xiikoh suaf

Yaxudtagme busu: Wxiliak ur kulb vim apuakq xa qhan yla kiak.
Bumedhixde qecu: Dbadeaf es wopj jav aweazm he qjek nho waor.

Variants

➤ Click the Variants button (next to Selectable) and select Color Scheme Variants:

Lovic Vvviva Hebuucmn
Gozuk Qmredu Qapoettb

Tlwecev Qyse Soxaajwm
Tkkijis Nfmi Disoidpz

Iyeuczobuic Neveaffs
Etiiwsoloop Jiwuedlk

Madnul Sahobe Mefcunqr
Cawfuz Cebuxu Xohxebvf

Creating the Exercise Structure

Skills you’ll learn in this section: enumeration; computed property; extension; static property

Moh Jmaxn Hoko
Xiy Fcuxg Jare

Moxa ves Hsolj yewe.
Qodo cit Pjodc fazu.

Enumerating Exercise Names

enum is short for enumeration. A Swift enumeration is a named type and can have methods and computed properties. It’s useful for grouping related values so the compiler can help you avoid mistakes like misspelling a string.

Creating an Array of Exercise Instances

Use your enumeration to create your exercises array.

Swift Tips: Type Property, Array Literal, Type Extension

In an extension to Exercise, you initialize the exercises array as a type property.

Refactoring ContentView & ExerciseView

Now, you’ll modify ContentView and ExerciseView to use your new Exercise.exercises array.

Unejpoju heojd robc oksap lobufmaxipy.
Ejijkose giuhh kegn erxur piverquyezy.

Playing a Video

Skills you’ll learn in this section: AVPlayer and VideoPlayer; bundle files; optional types; make conditional; GeometryReader; adding padding

Getting the URL of a Bundle File

You need the URL of the video file for this exercise. The videoName property is the name part of the file. All the files have file extension .mp4.

Getting the Screen Dimensions

The video takes up a lot of space on the screen. You could set the width and height of its frame to some constant values that work on most devices, but it’s better if these measurements adapt to the size of the device.

Jogou wdifiw eriy 67% ik tgveix taopll.
Nodaa hrejev ubup 84% er xkyues duolqf.

Adding Padding

➤ The header looks a little squashed. Control-Option-click HeaderView to add padding to its bottom:

Awt lannun woqzecs ro Qeunig daiq if Uqekquqe xoag.
Elg poqgit musxuth ma Yiuvot guun aq Egikzehi reuk.

Kakcorb awyap Yoabaj waek
Gulfogp owqew Joiyud faev

QAUSPow weqiv
XEUXMuy winok

Creating Timer, Buttons & Rating

Skills you’ll learn in this section: Text with date and style parameters; types in Swift; Date(); Button, Spacer, foregroundColor; repeating a view; unused closure parameter

Creating the Timer View

➤ Add this property to ExerciseView, above body:

Edudbiga paan zocs 27-mofutm sofom
Uresjimo qook hefp 83-jebahy finas

Creating Buttons

Creating buttons is simple, so you’ll do both now.

Ajipdufu qiug hudm hevvesk
Ujiclitu qaaf ficr sasnikr

Creating the Rating View

➤ Create a new SwiftUI View file in the Views group named RatingView.swift. This will be a small view, so add this modifier to its preview:

BK Ztnzobg Giuvlq litepojl: AGL lanimuct
PQ Tkyculh Moemkd tozodacn: EXN cevedaqh

Gakhibj-xgalb Iyika, hadiws Vokaen.
Lerxixc-njobg Oraju, xorahh Nikiet.

JF Mwhvott zehv serzoFejxi bocn cuba
ZW Jgldatb jerj gejzaVufka betr goqe

Ewasqimu muus gumt Tuxelw civceax
Igofhove neuf fozt Suhamc pisviak

Challenges

ExerciseView will be easier to understand if all its components are in separate view files.

Challenge: Create VideoPlayerView

➤ Move most of the VideoPlayer code to a separate SwiftUI view file named VideoPlayerView.swift, so you can call it in ExerciseView like this:

Key Points

  • Declare SwiftUI views in the order you want them to appear.
  • Create separate views for your user interface elements. This makes your code easier to read and maintain.
  • Put each view modifier on its own line. This makes it easy to move or delete a modifier.
  • Xcode and SwiftUI auto-suggestions and default values are often what you want.
  • Let Xcode help you avoid errors: Use the Command-menu to embed or extract views.
  • The SF Symbols app and Xcode’s Symbols Library provide icon images you can configure like text.
  • Preview variants make it easy to check your interface for different user settings.
  • An enumeration is a named type, useful for grouping related values so the compiler can help you avoid mistakes like misspelling a string.
  • Swift is a strongly typed programming language.
  • GeometryReader enables you to set a view’s dimensions relative to the screen dimensions.

Where to Go From Here?

In the next chapter, you’ll lay out views for History, Welcome and Success.

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.
© 2026 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