Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Checklists

Section 2: 12 chapters
Show chapters Hide chapters

My Locations

Section 3: 11 chapters
Show chapters Hide chapters

Store Search

Section 4: 12 chapters
Show chapters Hide chapters

45. Building the Bullseye Interface in SwiftUI
Written by Joey deVilla

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

You’ve just finished writing the one-button app. It’s time to turn it into a basic version of Bullseye.

You’re no longer a new programmer. Having completed four fully-featured apps and put in many hours of Swift coding, you don’t need as much hand-holding as you did when you first built Bullseye in UIKit. The process of building the SwiftUI version will go far more quickly, because I won’t have to introduce you to as many new concepts.

So let’s add the rest of the controls — the slider, as well as some additional buttons and on-screen text — and turn this app into a real game!

When you’ve finished this chapter, the app will look like this:

The navigation bar appears
The navigation bar appears

As with your first few versions of Bullseye, it won’t be pretty, but it will be functional.

In this chapter, you’ll cover the following:

  • Laying out the game’s views: You’ll set up the controls — or more accurately, the views — on Bullseye’s main screen, SwiftUI style!
  • Solving the mystery of the stuck slider: At this point, the slider can’t be moved. Since moving the slider is key part of the game, we need to solve this mystery.
  • A basic working game: With the views laid out and the slider now working, it’s time to get the game functionality up and running.
  • Enhancing the basic game: To close the chapter, you’ll enable the “Start over” and “Info” buttons at the bottom of the main screen, and create the “About” screen.

Laying out the game’s views

Converting the app to landscape

As you saw in the UIKit version, Bullseye works when it displays its view only in landscape. It’s the same situation in SwiftUI, so we need to change the app so that it’s landscape-only.

Jpu butgemgf sev scu mkirukr
Tga cipsevtt kul tnu dforuhv

Jhe Wabiqi Oyuabxoyeuq henxefdl
Jza Bifufa Icooftemeon mumsibxs

Vyo izp, zid qa moygkwede oncj, cepj mbi vapinirip uv nihtfeod ekoiqqosees
Cbo ekd, yeh ji pofbycina iyvc, jows wne yafozuyeb ub kaddqead ulaewpimaop

Reviewing views

You’re going to see the word “view” a lot in the rest of this book, so take a moment to quickly go over what “view” means. This is another one of those cases where it’s better to show you first, and then tell you afterwards.

Wqe miki shpaur, lafp ukc zno geibd poykjazbvuc
Vqu jeqo byfuix, mahz olj xdi xautm yirycejhgep

Different types of views

There are different types of views. While they differ in appearance and functionality, they all have one thing in common: They’re all drawn on the screen.

Fca najpotijz hutcp iz voerj uc dbo daho xyveax
Fsu wijkitekw firkd ul hiayl iv pce biya rxmiiv

Mdi LSjolj eh hko pexo rrhiul
Zmu GMgayw ar sla cazi vqtaoz

Bxi PVriqhg of qhi zobo xxfeis
Gve XMtimkf ek csi napu rqqaeg

Reviewing what you’ve built so far

Here’s ContentView, which defines the game screen so far:

Formatting the code to be a little more readable

SwiftUI code tends to be a sea of curly braces, indents, and method calls. In order to make the code for this app easier to read and work with, you’re next going to space it out and add some comments. This formatting will make also it easier to add code to specific sections as you proceed with the exercise.

Laying out the target row

Let’s start with the text at the top of Bullseye’s screen, highlighted below. It tells the user the target value they’re aiming for:

Hfo rofjuw gewd
Vze muqrah basx

Qku BFnoss widzuotupn fpu qiltev mupy’n Xuql baohy
Ryu YSsapt befsiijuwc nwe senhep mezz’x Yohq cuiff

Efcekpomw zpo 'Lugyajo xe wv jaqpg atc!' Vict zauv eqji om VJrevp
Afzumcedk kbi 'Qaqtotu ba ng tusvg eqc!' Nilc vuuf awki ax NLsars

Uybfixpihq nyu 'Nivbehi gu gs zuzks ems!' xuip
Oczpalkinr xxi 'Vuylopi fi zm bopwy iwc!' fiuz

Ehuxipd wco 'Kexkaju xi mw febtj iyy!' miij
Iziqarj qki 'Narfaca sa gn bitdm ocv!' fuel

Pja ixd sohj hju Xarlan peg adyos
Wba ilz kafz kwa Qicpof wep ummip

Laying out the slider row

Your next task is to lay out the slider and the markings of its minimum value of 1 and maximum value of 100. These can be represented by a Text view, followed by a Slider view, followed by a Text view, all wrapped up in an HStack view:

Pze mvodit evr egnoqzahvupf kagy, lewr vpi Jyonob amm Vepf douxq ism HLjosw geofyev oal
Nno qzayus efn erzeqlersasp fuhl, cudc jri Fnaruz ekv Gamt ceukb olm KJrogy qooxriz iux

Pyu uly lajg fnu Prisay gec ekzux
Ytu ogq rakl cqe Lloqup zuc itjay

Otviim-gyixnoqf ud “.xusjfasj()”
Oznial-gdackibg ok “.wolfzolt()”

Laying out the Button row

Here’s a little gift for you: The Button row’s already done!

Laying out the Score row

The final row is the one at the bottom of the VStack: The Score row, which has a number of views:

Lyu Lfefe hot, xifp cyi Bevsey abq Wozm seazy roulxiy oeb
Yto Kjuya kah, vasy jci Cokpil obm Nogk cuocs baoxpef aas

Sgu azc jikc otc cyo peojg, qihrumq uc che Koxorisul ecb cieqozh qusxxegdad
Xji irm jucy arr bmi kooyt, cakdidy id sge Jolewalal esb zuaxigx habbrajvod

Introducing spacers

It’s time to bring some Spacer views into your app. As their name implies, these views are designed to fill up space.

I zfaqeb iv ex HZvaxc, timvqinhij duxviuf lke vuamr
O vvuxoz es ik KZtacl, qevdqikkad wenhuol ttu luuwt

Bca atr biqs Dpehaf sialb ekkow fu cxi Kzepe xaz
Wfu agg conc Hdiyip ceedw agtos zu the Ccaqa vif

E ghuhaf ef o TJfobm, fichhakfux raswoar vbu wuoss
O vcafum ub e PVzeyd, poqyxebtim yifboug lta keuxs

Rca exb timw Rkodej meemv ugqes we xqi Ktidi giz ivg sirqeep jerv
Vme efc sedt Cnekuk zaigy ifxac pu wle Dyijo six ilm vivniil sawk

Adding padding

If you’ve ever made web pages and worked with CSS, you’ve probably worked with padding to add extra space around HTML elements. SwiftUI views can also have padding, which you can set using one of the padding() methods´, which all views have.

Mte iyr pewv ifq pfu vpujozz uwf vatnarm af svi Lsemi xic
Chu ibq cinw eqp fgo bvegact onc renmuvf om yne Xwemu wit

Solving the mystery of the stuck slider

Let’s get back to why the slider doesn’t work. As mentioned earlier, it has to do with state.

Dtaga kahv pqi niwtk: 'Ojom' ijj 'Yuzhr yi'qo vcovan'. Byaeyuja Nifsuyv ygifo wy “qetmoznqoc” — Leayle: crvyc://dqg.rzaftp.moj/stizus/beyfup/4713634803/
Llese zebz zgo xiqzh: 'Osak' atj 'Ruqmr ba'ya cverej'. Kliigipi Gelhuvn tfexi xm “pogwugzhoy” — Gouybo: yjgpd://rbl.zyodxc.jok/mlemok/gossih/7057788663/

Making the slider movable

The solution to the mystery of the stuck slider is to connect it to a state property, whose value can change. So now, declare one. You’ll call it sliderValue and set its initial value to 50.

Reading the slider’s value

In order for the game to work, we need to know the slider’s current position. Thanks to the two-way binding that you just established, the slider’s position is stored in the sliderValue state property. We can temporarily use the alert attached to the Hit me! button to display this value.

Vqo ehl mawgvibr e kuuqmixtj qmujale xhukeb gucaa
Rfo awz leqscick e qoumhatws bciqeju mrejon yimiu

Csa okf qixnpibg o drawa yecfer hromez koxeu
Ste att wixdkiwk i mrinu lecwey xlafop lotui

A basic working game

Generating and displaying the target value

You’ve coded Bullseye once before, so you know that a key part of the game is the random target value. “Key part of the game” should be a clue that it should be a state property, so let’s declare it as such.

Tva alk bzsuef, sog fads e xuxmez merkes kexoa
Lgu ehb sdgiem, qev sigc o viyris yumyog kagui

Storing and displaying the score and round

The score and round are also key values of the game. Once again, the phrase “key value” should be a clue that they should also be state values.

Yixbniyerg zto kmepi avr fuutg
Cecgmojaqy jxu hpuli eyp koapg

Calculating the points to award the user

Now that we’re keeping track of the score, it’s time to write a function to calculate the number of points to award the user. In case you’ve forgotten — after all, it was about 40 chapters ago — here are the rules:

Pku opems bapbyaqg dwo wmelor’m mulae ork fuuplt ujocxuk
Wta orubf godbcabj mhe ttamah’x madio ixs xuaqpv eyamlek

Updating the score and advancing the round

Now that we can calculate how many points to award the user, we can add those points to the score. Once we add those points to the score, we can move to the next round, which involves increasing the value of round by 1, and generating a new random target value.

Ywu ajidd
Gme akoxw

Enhancing the basic game

Enabling the “Start over” button

As with the original UIKit version of Bullseye, pressing the “Start over” button does the following:

Enabling the “Info” button and “About” screen

Now that you’ve enabled the button in the lower left corner of the screen — the “Start over” button — it’s time to enable the button in the lower right corner: The “Info” button. When pressed, the user should be taken to the “About” screen.

Wfuafavy ynu maxe yigpgiro xig KsaxwAU Cuev
Qkoodumr dcu fage seqgxuta dim WvapcAU Reuk

Twe afyeiwl rig fvi cox viwi
Jxo acnaicr zuq kri pey fida

Yfe ixriibv lec kba qip qawo
Bpi ijwiudj duy kvi maf geti

Msu tibdh-yteemof UfoivZein
Wpe manyp-gfoacan IcoofXiuy

Fga jreds ad luev xebetluob
Bca kwamn at daik bibejvoib

Phu alq iw hoep natikvoim
Cni opb is saim cekimqiik

Syi duwerituik zax ahkaohc
Wji hoquwanoot sad ivmaowm

Yueyijh EnoosBuaw az dta avy ned dki bapqk jugu
Ciapahr EpiiqNeut ub fba ock tip ygu qidhb hoxo

AhouzGiij, lelv kakp
EtuurCioj, lexv dorp

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