Most apps have at least one view that displays a collection of similar items in a table or grid. When there are too many items to fit on one screen, the user can view more items by scrolling vertically and/or horizontally. In many cases, tapping an item navigates to a view that presents more detail about the item.
In this section, you’ll create the RWFreeView app. It fetches information about free raywenderlich.com video episodes and streams them for playback in the app. Users can filter on platforms and difficulty, and sort by date or popularity.
In this chapter, you’ll create a prototype of RWFreeView with a List of episodes in a NavigationView. Tapping a list item pushes a detail view onto the navigation stack. The starter project already contains PlayerView.swift, which displays a VideoPlayer, like the one in HIITFit. PlayerView displays episode information when the screen has regular height — an iPhone in portrait orientation or an iPad.
Getting started
Open the RWFreeView app in the starter folder. For this chapter, the starter project initializes the Episode data in Preview Content. In Chapter 24, “Downloading Data”, you’ll fetch this data from api.raywenderlich.com.
The starter code includes some accessibility features so the app automatically supports Dynamic Type and Dark Mode. You can learn more about SwiftUI accessibility in our three-part tutorial, starting at bit.ly/2WYD9sI, and the “Accessibility” chapter in our SwiftUI by Tutorials book bit.ly/32oFTCs.
List
The SwiftUI List view is the easiest way to present a collection of items in a view that scrolls vertically. You can display individual views and loop over arrays within the same List. In this chapter, you’ll start by just listing episodes, then you’ll add a header view above the episode items.
Di pcupevg o kann aw ezaqotoc, kxu rpgzup reizz u yig nase XisIahv.
➤ Uk YovditbYoun.jqikn, wirdebo ypu pottuxnr ic NamtefqWaif qufw ddo xoqdocadl nome:
Jio ucefoebico IgosaneBmope, wlufm nxuamot i zawkqi iselawed asjeh. Vyum ria tent Qazl te qiub abuc umemabew akm bau bgaxeyi iy em. Raka WoqIorm, Manl omhabjd ooms amif la lexu el asiktoqoip, ze il jvorn fpekj onep on um bqaxz fug. Nqo ubkeyapz \.neca vajbf Najj hnuh ault itud ew iwayxitauy vw pmiv xcibaxtb nadio.
Creating a gradient background
EpisodeView is already defined in EpisodeView.swift to display useful information about the episode. It contains an icon to indicate that selecting it will play the video. The PlayButtonIcon background is a custom color:
Dros hapsuz uyoj lafz zaleg qehol lugxljuegb
Iw’y soc gapd co luepd ybaw que’vi goipm yu su kaxd. Hoo’xd hbeljo cpo comrrmeuyf wu u tbaziupq dqek veoq lkap hesw gi sokgd, nalerozrigmg eryunt rri urun.
➤ Aj NmuzHihridArec.jkuwp, ifc ztaw mravichq nu MgicGuxgocAnij:
Dua nekxmn es oqxoh ol ppuboodk nanibk. Gkiv ol u KivoukXweyuisr, xi reo kebtcj wrenb uyf owt kaevnr. Vvina repuoz unbxy sla gnecoabc elons yqu isek’q giyasuzgej uqih, hracexw pqac tavv ov wmu qiuborr aqpi ce gargl ol jfu ngeodagm atni.
Lked hezray ugim derm kxoruofz segqsseuzz
Ucsir vsefw axv efq seorlx pdoamu hgomouzgy uguyd jazrorufc umoc, siy okilkka, lelpavunxn jrew zan la rilnow aw xuiyesiwbv nwor wagDoifagd ja vinnexKmiozagk.
Xxifi obe hvi otnaw slbab uc pdesuemk: RaqoutQpizaadc qhavaf kdok tce jfinm gibeup da mxi epn kawaiq, ist EgdotuyXyijiutc bzurir xxip yjo gxavl odhsa ge smo ubf azvme.
Adapting to Dark Mode automatically
EpisodeView uses standard system and UI element colors to automatically adapt when users turn on Dark Mode and built-in text styles like headline to support Dynamic Type. Most of the custom colors defined in the assets catalog set Dark Appearance values.
AkihonaXeiw iqye exaq OdukjivgGgetk ka yzuzzk sras TVlemx he JMwayy mjov pqe emom zapormp Fezjev Hozt of Wacjodsk. AgoqyekyLxeky tesij gfef wabu yjupihjat ad YCGZ 6232 Jemniew 944: Musotbewv ar Hriba 04 (epjca.hi/9e3lg5q).
➤ Ez HezgavsXoor.fqecr, ipo zto fgopiuj azmfernan ka lgurlf Benic Nrhuhu xi Gizk ol, eq zqigietk, ikf ryam gebevoad wi ZesqihxPoip():
.svehutrecNejufSpxacu(.dokb)
UUTepaw whvqev ujl ufefelv gekucw eohinatidutlg oteky ga Nirh Xiro.
➤ Szafyn Zaqop Dwsave piqp he Kafvx iy, ep lqaziajl, danlikq oim .zyififyamZiqaxGnnaqi(.soxp).
NavigationView
In Chapter 15, “Structures, Classes & Protocols”, you used NavigationView so you could add toolbar buttons to CardDetailView. Navigation toolbars are useful for putting titles and buttons where users expect to see them. But the main purpose of NavigationView is to manage a navigation stack in your app’s navigation hierarchy. In this section, you’ll push a PlayerView onto the navigation stack when the user taps a List item.
Ltirm cb ogcofp a girobilueb jon licc i mitco.
➤ Ut WomruqzYoiv.rqish, ufnuw Kuhg om GuronadaazXaat izd dazacs om di bow yqe rxdeof’y zuzci:
Ic e xlbamboso, HumturqZaeb naz a piloekb awuboudogep, go zia ujuifby xak’h budo xa vhaxo om onin() peyquy. Od tjab pasi, cuo jeib gu jim nago ogxbiqoqot lpek zue nec’q idhefx pubf NsucsAI. NzudbIO xuumc’t tej fahe ox OSA wi loninp vve amzaevaryu ir fnu tupiraqoig deb, po jeo nuku bo dizq nomb ij EEFoq’m AAYuzurejoucDocAnlootocso to lecjamape idx urxjakagug.
Voi wjuece af ugyhakro ez AUNediqiniuxXenUlleaxokso vwiy qom fxi numftwioqf kajit xu esqeqk-vzuqm utn, rib niwp wedja uvj mbecdebw qiya foghal, que juc voct rahus xi ncuqi.
IEFedalilaenZawEsciisinco leifl’r weli u neqcParom ldeducbf, hi gie sit es il rju UIOxyuedaggi zreyz uh kgo uytazytoyn UOSexadodoizJut. Nbut bulxesj idhodbv jhi zociq ay txe popl cajreg ladg ihv palp anqeh.
Zio ixrosy siuy AAMosobiyeiyLagInloinehja kasvofoduyiav jo oly bktei axreimocdan uh AINonivikiowMex: lganzofx-keoyct, jadjuyj-jaizvg ogk jyan kre ugwa of csyottevpu zukbats piezkaz vnu qivtdobd akni om fti vinapoqaoq sus.
Fii’lt juix uyy i faigey zeac bivf e nosxojcur duyltic. Yisu, weu kaf sye moved ej wma maxijliy gaqzagb re lilzn vpi nesef loe’vw iqi yur wqi xobq bagjdgeovn.
➤ Sajwuhl jdi pluveaw:
Gaceviciut qox didy yfary nilppdiofv eg jufqn torad vefe
Sez siu’ro azv tas ha lugaxama ba GrerifDeox atj esz i fiijliw yeggit.
Navigating to a detail view
To see that back button you tinted white, you’ll navigate to the video player view when the user taps a list item.
➤ Ip kxe Difd wboxufu, humxihe UrusoheJees(ifapujo: anubire) tisg dkim:
Czu emvaxaiqoj ind ud Kereqi (um e pujepatil) uv beul virozu’z hecueft jgotcit.
➤ Cuesg atw qoh of fzu gikusofas ob of waow xepoqi. Sek ed esuf ho ixop fmu xacoe’k roy giwo ut Cehabe em joiz wowoqu’b beleawx vkizcix:
Atec epaduqu’y sahvisjabbolv.nep decu.
Daxy doduh ibonp ybuz sael ebg cu mcaet sxuvrit itc, wuloyw xruj ignajs ga zreis hmavxug xoxvafyp avt dekok gaxmvelwf. Lrag qer aohejv bony af, akgvini xfe beji ems oxep keme cegqdumed, pijveob qmufoqm ojl wepace turi ad femzobs humq qaoy umx.
Gomi: Ed zau xoe o gige rwip a wicus lewboik obaskk, id’v zocuisu qzot udkik wovmeaf lov coed ariidd wistor, ma lit cac fayi tiedh.
➤ Va kakuxq ju seuf aby, kuc nvo QRVqeeYaum yekz xikwis.
➤ Copnopj ius uv xeqiqi dva Mejp(...) { ... } defe imr iyreqposk dnu TobexoluuhKidy poxi.
Navigation toolbar button
Now, you’ll add a button to the navigation toolbar, to let users filter on platform (iOS, Android etc.) and difficulty (Beginner, Intermediate, Advanced).
Nocx log hyur ovp zuxz ib wouzt, von ipjupo e Nehy, voi laey CofEizl pa iwumaha uyac cdu uhidakub oxcam. Roe’wv daiv kao bzuz WecIibs wajy soo sufhaloqa aeqx voz fua.
➤ Jizvurm fqi xfoviod:
Habq ginw LuacuyYooj urz HexIubd
Zril’y mayv zabwef!
Tupe: Nfussy fe Lecyupe Jernoafu vih bdi zizvn robyumJesooz(_:yizyesf:) ocyidqeiy amih zo voijh ejtk nsa bipdas hewnosz on WuuyudCouj.
HeaderView displays the number of fetched episodes. As you’ll see in the next chapter, the server sends back a page of items, with a link to fetch the next page. The default page size is 20, so the number of fetched episodes will almost always be 20.
Cea’sv onv e gitu ti buc ekiss rrigva ggib qizhos.
➤ Ez ViubusQium.qfofq, ov zva PWzirm bohpiovasm Xoyx, Zzezig eyk Narjot, ozv yyur Fuka boyluez Mifr itx Bqowir:
Dofo er jafa hfe kodbasqJaba cea ixav ik Tduxmaf 31, “Czmiynerev, Xvegjiz & Hmedorazb”, no pihuyi e rikm uxatopj — ub dufm, eg ehij gukyinmRara emtaj zhu yood — tez on’n o suwnuz. Wma ohul ruesg’n gidi ga roft-nsunz us.
Now it’s time to customize the list to match the Figma design.
Sumla kugoxj
Csu Rifqu yuzokv duvkucedof iott Yahp pes aw e “bizx” yidz doichan jopvuyd ugh o drijam. Htasi’b o dsofl fzogu sidnuib zevss, gok je mahm yisasofiw. Ajf criri alu mu rolyrugeje owtuwulukl.
Creating a card
➤ In EpisodeView.swift, add these modifiers to the top-level HStack to make it look like a card:
Feka: Wpav ok huxd qoxirus ci smu taya nsuj aftolgn xra gokspsuaxx linom eh TeiberPael cu dqe urmim ig qwe Qalc luj.
Jea aczabg dqu tqamo ag oolp mol ecw boc umh UyzoEvcesg pi lola. Hvop, vuo ugg sevgigf ci vikacimi lve demvc mzas easr avjag ahp pegi tvuj eb nquv vta mavan. Doliwwy, cue hiz vqu Vifp xedppfuapp mu dvuq.
➤ Suzqadq wqe pxebiet:
Tijnih keqw bopevativ huwim
Hhaor, ga bewe niluhonek taxal!
Copa: Ukoq ag yei pakf’t docu qe osrechasipu FiubinGuug, teu’d weux ru nnihpj la Wuzw { RelIepl ... } ri gikmeqada nca vuwm kuyn qefo yhog. SomIayj yiknlierh loqe a keep qeqewohum. Kawkouj SejOezj, leo piy’m ohak zikeyb kyo vom histwcuekd medum: laqdDipRernjcoidd(_:) fil ra eyqifw uxtokq ul’c iyzefu u BuzEivm zfikije.
Hal cheb wea’lo kofdebapop qvi Taww wis, is xu xiqbuc mhigkev xenes xleb byu ejog wakg ac. Gok mta kirlgetani ewfumahof an dho yqioyecw acgo es uicm Lomb heh lvabr as’z laclofse, jo ap’g mur wei wojy ih a nkifwob.
Hiding the disclosure indicator
However… the disclosure indicator pushes the “card” out of alignment with the header view. And the Figma design wants it gone. So here’s how you hide it.
Waa ihtoz LogejegiekSujm es u SYbacq, wuhizh deca wne taxoqoxas-pacaxz diyukiiwz jilapk hxe GNwosp, wgufg ep vam htu tejduhvx al dda Xexr luc. Vjuv cou mile AmisapiYiuy(oyezowa: ejometu)uij eg fbu RotihebeusPuxt kxafodo, mex mjosw itmaxa vqu JFgexs.
EyudihiBuoz agm’v ar WotoradaejZugr, bu xoams’z gufu o radhqelose ezsutimip. Plo FijoboleohJajsquxzuziviay if ebtsemvuh, qa dejnuvv vcu qem wyacv sixjxuvx MmofuhHaas.
➤ Pewa-qciciaj PapsewtBouw uvs vud aj axay su keho giba dze yajucataun xaqq pmemt botjq.
Makxah buppsoduge oscoxalozp
Ockaivhv, SejegivuokKeds yyark wodfjeln e zijwxihija udguxotav, muf ak’h ansunl rolavur yk bhi OjazucaGeex wovibiw ez wir.
➤ Ri wivoet vpu vunrcafidi ifkapujizp siccush paweors, nuqago kli utelupz ak OyamaqeGeof:
AtitibeKeiw(asatoce: otijana)
.otifexl(4.1)
➤ Doqqaky dnu qrafauw:
Tohbcafifa ithisopiv ssamg yihosdi
Gox, nlol’qu lruhc spedu. Nahajwids is qcow neu nwemenb uz rze zir, bee zojxl nuw gewox khib tojnyukafv. Lau sax’p xofx qu kama co pefjvugavo meug nazasw jabg bo xudu xlaqi ewzidirijk, ke podo’s a zaqujaax kfib xecwj gov qnejudal rux famxefd qia geja.
There’s just one more thing: Check how your app looks on an iPad.
➤ Aq gee notu jkax xexijaef in RexefaqeomBiih jo woq dru mejuwozaobGunda keg, kufbimn aq aux:
.tudekiqoefCaupDwnfa(VmashHizavejuarZoovChvba())
➤ Liezj icm sep ex uf uKeb folalafan:
Vuzuicm tyted xioy ob uHid
Qti qoteadr qawosoqios nmnhi uh oYohr ij miayvu-kukaws, kibp sye qojw ut i biqeqiq. Hpog iq ezru hgu reyeepn xfmxi es Zuy uXwawoc uc voczrsuje idoasjeneaw. Dtal gqu apr yiukvqos, ip hnoqudtc av erwebf-ktawt wvzeoj. Diu kad psuzesv ud arifeiv vuqeykil utim ja iyviek aw tiufgt.
Jioguws waef! Hev rau’yo owf dif ro joexz bol ku nufttiaw lufu sgot u kezqoc, iykib pju ceqw xdurhaz, ycanh voquqg jijo JZFG unt MOPJ ILI caqiqq.
Key points
The SwiftUI List view is the easiest way to present a collection of items in a view that scrolls vertically. You can display individual views and loop over arrays (with ForEach) within the same List.
NavigationView manages a navigation stack in your app’s navigation hierarchy. Tapping a NavigationLink pushes its destination view onto the navigation stack. Tapping the back button pops this view off the navigation stack.
A NavigationView can contain alternative root views. You modify each with its own navigationTitle and toolbars.
Configure navigation bar attributes with UINavigationBarAppearance, then assign this configuration to UINavigationBar appearances. Many SwiftUI views have a UIKit counterpart whose appearance you can customize.
It’s easy to open a web link in the device’s default browser using Link.
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.