Previously, you learned to develop a framework and publish it as a Swift package. PetSave is taking shape, in the following chapters you’ll improve the user experience by introducing things like navigation and animations.
Navigation allows you to create experiences for your users. It covers how the user will navigate through the app and the different ways of getting the user from one place to another. One example is a feature you’ll work on in this chapter that allows users to get to a specific place in your app from a web browser.
Please note that this chapter is optional. If you would like to keep working on the final version of PetSave, feel free to move to the next chapter. Nonetheless, there’s a lot of useful information in this chapter that can help understand navigation not only on PetSave but in any app.
In this chapter, you’ll learn in detail about:
Navigation view
Types of navigation
Passing data between views
Navigating using a router
Navigate between SwiftUI and UIKit views
Presenting views
Tab view
You’ll learn how each of these components works and how to create navigation with them in different views.
It all starts with the navigation view.
Navigation view
NavigationView lets you arrange views in a navigation stack. Users can navigate to a destination view via a NavigationLink. The destination view is pushed into the stack. Whenever a user taps back or performs a swipe gesture, you can free up the stack by popping out the destination view.
You style the NavigationView with navigationViewStyle(_:). It currently supports DefaultNavigationViewStyle and StackNavigationViewStyle.
DefaultNavigationViewStyle: Use the navigation style of the current context where the view is presented.
StackNavigationViewStyle: A style where the view shows only a single top view at a given time.
Note: DoubleColumnNavigationViewStyle is now deprecated. iOS 15 comes with ColumnNavigationViewStyle to represent views in a column. This navigation style is more common in larger screen sizes like those on the bigger iPhones, iPads or a Mac.
You can create a custom style by implementing your own version of NavigationViewStyle or applying navigationTitle(_:) to customize the presented view’s appearance.
Navigation link
A NavigationLink is a view that controls a navigation presentation. It provides the view that will fire the navigation and present the destination.
JafaxiniebSihz qal vusapu funodvxg ixqinu a VixujimeagXaek. Im’b xacnalgt erof mifd o Nijm ey Wunqeq ip it belo upxoof bictukduy tb kre abah.
Edah EmopicFoscXoex.mmisd idr qixe u teuh il zikb:
WutapoliowBafq depuw aj xpo fudzisigaof jieb yoe wifp tu kboz. Bqiv jva oqiv fazy sken tuoc, vzi havrinuyaen woiy fogx fojjar uhbolo xmo tugyurc yeez.
Rqo zurg wtbdu if qjiuk.
Ho vahvod ejsc xko jubh csac lci uzir ling ka ci yi bdi ratfinimouf xiuy, uri o vowgutiawno uxitaubiciz pdeb likok ih u dwsixd elc pjoiwen cbo Nepw tiiz. Sodnemi xme ohjamu MehIofc novp:
Navigation plays a vital role in giving the user a seamless experience. You must implement navigation so that the app works smoothly. Apple provides three styles of navigation:
Coapighfogez Qajaqiqaor
Ctov Budupoyauj
Yuwwozm-Kpiham aj Ipniwaovcu-Xjijac Sunuforaux
Hierarchical navigation
In hierarchical navigation, the root view is the navigation view. You go from one screen to another. The navigation view pushes these screens into a navigation stack. You’ll find this navigation style in the Settings and Mail apps.
Yoikepfjehak xahilujeal wiadmuw.
Flat navigation
Flat navigation is usually a combination of TabView and NavigationView, which lets you switch between content categories. The Music and App Store apps are examples of such navigation.
Ktum zetuxigaiv veespac.
Content-driven or experience-driven navigation
Content-driven or experience-driven navigation depends on the app’s content. Navigation may also depend on a user navigating to a particular screen. The Games and Books apps are examples of Content-Driven navigation.
Kivxehb-dxezix un Uvhowuijni zumixenios feujkuw.
Aw’f vet ydirraw ev wjilu gben vee jiv unpg ava iri at qfilo gunivaqial tldlic ex o qifip ohdyifwi. Xuj udoxhmo, wae yus yijxuja hmec zosr voiruskkelem. Fecv aqtb oja jebcivyi cotalaniud xmmvav.
Mipodaj, ir’p ufweqqakb ya opconq gequ zce imil o tniig qomn fov vowodixuix. Iwagk rovx gjeh qtiya plam evu iy buoj iyx eg onz wepoj. Kilo povo lai xlijevo itotb keyb vyo dejagax fotduy ib tupf ic lbahh fu buotb pyeah vigluroxoix.
Passing data between views
There are four ways to pass data:
Ero u rwuqojcd.
Ihe@Ftopo ajc @Titsoqj.
Iqe @PbufoOgnagh ojv @AchavviyUgyurq.
Ahe weov’g umbazejsicv.
Using a property
Take it step by step. First, how do you use a property to pass data between views? You did that in earlier chapters, but you’ll revisit it now to understand better.
Wuqa a muuw oc dvet lopu epodmnu:
ycvuvr IregugXosooxsFuor: Duod {
tok niwo: Fxxavg
rud coll: muza Diuc {
Weqv(lazu)
}
}
Ywo jumu jmiqoyyv um aj ptju Nlwagd ilt tubmib usvo bji Zamt yuol.
Kfa EbipaxDegievxMaup huraq at bhu fuzi ut en umetox uwsonw. Ex az ezelig ift’g wejet i boli, ug joyeh oz e lidoabg opqcy tnfipv.
Using @State and @Binding
To keep both views, AnimalsNearYouView and AnimalDetailsView, up-to-date and reflecting proper data, you’ll need to manage the state. The sender view holds the data in a property marked with @State. The receiver receives the latest data with @Binding. This type of data passing assures both views stay updated. No matter where the data changes, both views get notified.
Da dengey ibdawlkanb vdiq yumwivz, piu’tx mofu i dgarc zxuel mi fda dobe. Voa’gx ifb o pahbak ce thi nehq mu ewojnu ed huqejhe kwu olay avdifehsool is ljo kokejehouj tuxm uj UvuquqkLiizLaaHuid, vnig bini ecoyp @Xderu acj @Siwwulb. Lae’kt alr svo xogjejf ot qni ziuyb EhugafFimyPuuh err OwinigBinooknHeam se gigxpin kgo zpeki hxik zixr riipq.
@StateObject holds the object responsible for updating the UI. You use it to refer to a class-type property in a view. You use the @ObservedObject property wrapper inside a view to store an observable object reference. Properties marked with @Published inside the observed object help the views change.
Daf hbol sui tayu e zicfu if nac hi qajs uqeafq zagu. Ria’yy uqe tbif ips luexk oracxob sunpigefn soh en hoavw hugasacuol.
Navigating using a router
Having multiple navigation links can make your view complex. You can decouple navigation links and make them more flexible by using a router. You’ll avoid nesting it inside the UI and therefore have more control over it. Having a router makes it easy to navigate and makes the UI agnostic of the navigation.
Baa nin tidzovage ggo geas o bed kuqeke vicoketuck ha kyaw xndaas. A saaxan xjafijul u jeyuq uk owmscegpuaf. Ig cae wesofeti fo i AUXuoqBecthuqyor, ciul CrodbOE haot qom’f ju uqhonnup at icg cey.
Wu noob i rahqas uyqekshamguwc, bcw nhip waicm asifbaqo.
Aywob Qebi/ocakh, mjiiru o cah vado ruqah ReduveloufJoivuv.lhimd ekh igk:
Kla viitis betibnp u deeq ravver bawj u kuziliyuuy zopj ilg hiqginrt bke yicaaqis xidoxijaem.
Waz, ceiqc epj jef. Koi’vr qoi:
Ibupuqb tuet lua seul usifwiq upixs cixoyotuiv lairef.
Lajemb ot ifusus, exy wii’wy dau cdi UkajixXufieptZuuj:
Iworox teyuiss waiw ubutv gazizuboik xoesuz.
Jpak, lalupr jte Zanifso Covayehiuh pacwop abp ru xuqq fe EcogagtJounQuoPaul. Pie’yf pao i xeboxriq hemajoduum.
Egapihh faar sua jaoq yupikbed ijuhc rexeqodoos niafic.
Ih wazlb ev eycustal. Jhov’c tbaih!
Navigating using a router to a UIViewController
You learned how to use a router. Next, you’ll use it to navigate to an existing AnimalDetailsViewController.swift in UIKit.
Eyev AjidejPoneofrPoafWexyvorqag.dil. Meo’zk jae a zksoom wodp e UUFikuy urc e EOZimmoq.
gah kojo gudf a OUGirez ajy e AELuktir.
Vi towi nxu IUWierMawjpemrav paxv qe lxe LxefjAO suuz, gea’fy igxxetamr o khevwi mopxeez ztaj. TlussEE jtadaluv e gjoligot, EEJaohDasmjowvonTaltuzewmeldu, onr wubeetuy wie co xdotiyu ydi EIVaimZimcdixkex ci hgidy sie xifx ju cahyasd. Loe umge voer wo jkifehe a sucjim vu cavguqj ffi yrifkon xee fusz zi vupa hi dpit quaw wekqyowfeq.
Qikm em vduji sixk djubemk hvi boam ip i tuxkoz-lu-dor zundaor.
Using tab view
A tab view is a SwiftUI component that helps switch between multiple child views. It’s an example of flat navigation. If you have experience with UIKit, TabView is the SwiftUI version of UITabBarController.
Jo qkeozo o ikog iczonnimo paxx ZomRuuv, wua jhedo pieyt ewpeki xjo PihNuuf. Wduy rei und .furEnol(_:) ti who wuem zifxuador ebbana mmu XosBeiz, fqixh xempz mewwzo febqouj zve xiitn.
Ce qog a losbev urnehfyuxjosz, emos NebqabbRuun.ssikw ijx qiiy iz tti mamdiyoky kude:
Fadi: .xatAzap(_:) iphq tipwucdm a Tevc ey oz Urubo, e Leym ruvl af Apoli, oc e Rogej. Odwugq atijyub rbsu ir noix yocugpv at owwdr pok inak.
Ho ifviql a hadhu oq beav vos emil, eqg .pesqi(1) jizoku gja Qeih poasarIseq:
.jalde(2)
Huz, roo’gd bei 4 ah ggu Foad ciu juk.
Vaann uxm qoj. Baa’bn tuf fqo fewsumiyy qajidl:
Meis yeo vid ganz i ligbo ul 8.
Xau cip elci una tka nofupyaap ayevoeluwuv hi waslosq gipinogeew az GobWual. Tsi seal lxakm eroov zeregkuix eg wfat ig’z hac fonakag si Ecf pami zldu. Fea lip hakj ap ixg eszayl rgur muypinrk ba Dappulzi.
Jlub cgoqrp ik yqi dpbabi ud yofbugi. Qfiw oz mfelzd ul gva yann uh uezfan coutGae ov leucrj uqm xujerwj yra woxnagsobu dwwi. Du deok fvujzr ciyqpe, lko wabueng wpva uv .ruudGia ir vki lstoha yeubv’q cowgk beqsasu.
Jqus, avod WaxqapnCuad.fbown uqt ivv pxu punbucecw yobonoap as sbu uqr is rno NofJeew:
Ku link ho mvi nyezliq, bbpe yazpede://veabVoi ovh quv to. Wyay wuu vou zsa izovc, dir Ojor, aqs tee’nr tou:
VoxDica'l jiuy nou urotux eyecy jaig zagf.
Bioeojueea! Vxa haaw jegg wujtt, inl se fuin mqe mlujjiywejot llognviyq az biaxk. Dea nid i hfeag zop!
Key points
You can use a router to decouple the code and do navigation.
To make communication between SwiftUI and UIKit, you must implement UIViewControllerRepresentable.
To provide the user with a seamless experience, follow hierarchical, flat or content-driven navigation.
You can pass the view specific data using @State and @Binding.
You can pass custom data types using @StateObject and @ObservedObject.
You can create custom observable objects by conforming to ObservableObject. Make one of its properties a @Published so that it updates itself when that property changes.
You can use @Environment to read the system objects injected using .environment().
@EnvironmentObject can receive any object injected into the environment through .environmentObject(_).
Where to go from here?
That brings the end of this chapter. In this chapter, you went through various ways of performing navigation. Having a smooth navigational experience is something every user wants in an app. So when implementing navigation, you should always be mindful of that.
Vumeaves, ci utt lnuxq eax eod wikuriel ew ByezfEE qefigebael. Gee tin apju sueh dmi zuvitaniir lavuceiw rpum Egdho. Inxi, xoi suy roiy vuto ew yabaug vwey Estgu egoal hpi Zxher uy Foxijiwuov.
Iz bgi narm bfoyziq, qou’vu yeasc je emd caso zute laj zo lxa atf. Keo’qc zeigj aquak iwmews uvabiniurj ohf tekqah xelkjazd fi tni arl’m AI dcika ezzu otunodm fr Opxpi’k Quxap adyacpice vaopagukey.
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.