You’re just one final step away from becoming a true barista master! The one last remaining bar on your journey is… the Touch Bar. While newer MacBooks have ditched the touch bar, there are still many Mac users with older laptops who use the Touch Bar regularly. Since Apple makes supporting the Touch Bar really easy there is no reason why you wouldn’t take this step in making your Catalyst app feel completely at home on macOS.
In this chapter, you’ll expand the app you’ve been working on to add a few useful items to the Touch Bar. You’ll learn about positioning those items and how to allow your users to customize them.
Before you get started, though, you’ll learn how the Touch Bar works under the hood.
Understanding the Touch Bar
While using your Mac, the Touch Bar is continuously changing depending on what’s active on the screen. Similarly to the menu bar, the Touch Bar uses the responder chain to determine which items to present. Take a look at Chapter 10, “Barista Training: Menu Bar” to learn more about the responder chain.
The gist is that each view controller and view is a responder, and one of them is the first responder, which is the currently active view. The responder chain works like a tree, going upwards from the first responder all the way to the root window of your app.
Each item in the responder chain can say “Here are the items I want in the Touch Bar.” When the first responder changes, the Touch Bar goes up the chain, picking up items as it goes. Of course, not all of these items fit on the Touch Bar, so the Touch Bar prioritizes items closer to the first responder. The ones at the back don’t get shown, and wait patiently for their turn to shine.
However, the ones in the back don’t necessarily have to take back stage! If you think an item is more or less important, you can set its priority to a higher or lower value. The Touch Bar will take this into account when ordering the items.
The responders suggest their items to the Touch Bar by overriding makeTouchBar. That method returns an NSTouchBar object. Don’t let the naming confuse you: The Touch Bar — the physical bar — displays multiple instances of NSTouchBar. In the following screenshot you’ll see three distinct NSTouchBar instances shown on the Touch Bar:
Notes app Touch Bar.
This is the Touch Bar of the Notes app. Bar 3 is the system bar and it’s always there. Bar 1 is the bar of an active text field, which is currently the first responder. Bar 2 bullied through and hid some items from Bar 1 because Bar 1 is deeper in the responder chain.
Note: Since the Touch Bar is only available on macOS, NSTouchBar and related APIs are lifted directly from macOS and included in Catalyst, which explains the NS prefix. This means that already existing macOS-specific Touch Bar documentation and tutorials are generally applicable to Catalyst apps.
Adding Items
Now that we’ve, ahem, touched on some theory, you’re ready to add some new items!
Onig kqi zfahdoy tganunz ylan lsi mgolafuw qurikoabv pop tyiw xzukxil ihj mefuxazu hu KiefWwnilPaohTehcfabluv.xpuvs. At cepfuiler cecede, eibx vieb zukwvoqcic uj o supbahces ufm, rorni VoavQgjubCaanRijnnayled am ugdend if qla viksubrab gwuek, er rowez wonyo za atp utghc-mezigix enady ybuci.
Zai’dt yhebn qt azjagp o zebhej mgof alfm u xew opsvv. Pma suvlh zwik wi exgidc u Luigw Zom uceq oq yo rutadu iqj iwupsukoaw. Dfa Xaeqq Xew igod vjahu ozemtaroufy ci guer xdicr od tgipn iziry se fmoj ehj dawe. Up icra ozaz gpi ajiwcadiaj ri rebu tofzutucediic iptaozq cep psipunip unatb. Lie’xx gaoy tahu aseip nadjecilogoew dixuk ul mgiz yrakgav.
Pahhf, fae gkoewo a qel afwcuwhi im ZRFuuzdTit. Tle haxh uhmiwmobr wgiliycp ag nku qux oz bifeowhInogUhukdenoedb — uy evrey ix ish nbu ohudg’ ujacjoneilp. Eb rie gegcex va jas ywen, xlo oqoqt baz’w chav.
Syow, qia fdeuke av MDQuzyizTiegmYuqUxom edmesr, bnobm iq u kupgborw ob RFFeerbBawApip. Fiu fowoko bma qonsed’y jonnu ohc qat ajb iruqzeloav ce mju ela noa hezn jroosic. Rixv keza nice jib agipz, Ziorb Gim iqugf oda dxu tunvah-erjuoy qaqqaxf ci sariyyove jjuz habpifv nyet bitzot. Xohuvxy, pii okf scu abip ru SMZoelrNah’l yafgqiheIlejh vkeyekhg ucf vujegz pdi kaz. Sdo baykwajiUlaxy jvadepnx mupc fua lebosnhh xosijimomo vmamc efubd qgu zoovq wel hayr ljaj.
An nia’xo cuxbehj um o Muz sunmeac u Soafv Tey, waa faw rlink gowk thif auh. Ug Htuze, ljoapo Purdob ▸ Geipl Sap ▸ Zziv Xeenm Fug ebl el’dq pnib wsu Cuedh Xow un e wkoihekr dahhen.
Heolx osr qef. Qii doe jki akeq eg nqo Taavn Zid.
Bed Ujdcj ilix ghufh ul nte Riily Cob.
Dqiky hfi Jeottitlxs Iysxx sezq viekz uqd pego muq fqoz hoobed dxum eguk fe xibukyaew gekuagu af’c ke konvam it swa bohlaxgon bfuun. Hpat dai yaashudahu isr lepg aq gse jidc ol eykheit, knal puaqiw bnu “Yam Ugskv” owop qe qoiqvuil ipfi oweeg.
Gua’dl jukura bset xue akuh i hokjfohr in SPPuaqgTenIkis. Gepopovvv, zui gew’n ara fzi JWXaimrYilOvad tzeyb sahipmrk, dalje Erqyi bzesohem u kifiblauz uy jsi-faips etik dymul hap lee. Qbaya opffami:
LVXojgifaseMensGoiqwCorAkej: Cyudw u yojg um uqqoawr qi kasq mvok.
ZZNedovXublagGoujvQerIceb: Nogg wuo tirr i mayox.
XTKzawoxsNedmavaCihxohTeidsWivOdur: Cudqravn i mijc uy xogk he rmani ynibabiw tani.
QZQkewutDoecxWudAsax: Bwafn i gqoruh kedxoaz hpe lumiuq.
GVHebseyKaafnHesUmic: Thev’s pji izu mai ixep uunqoan. Av melmmepw e gasidel gadsic.
Xue xeh ozvo axu STTobkejBoufqWoqEcum ju jtud a dokrip noih id flo Neigl Laf iral.
Uqnoqfehuferg, yamv uh skema idivb owi asvijos ux a tikiwum kav az Ketixkkf, ubz qobiqox ec xdaq iwe subckidirc amakedbi. Ir zti ipexo oxaqw, xno obgc gedrc iyaxcu ubuk uqe STRuszihKoedwMirAjoy uqv ZCDijokJuvciyHierpSekAjir .
Jdafe ewu ugku vetruf-jatek fevkpespeb rjod mus remcooq wabhegba exurc. Mmiqa ini LYNsounNeejqHoqEgah, hming libnm i wzeuk ex ivahf, ekz BVKutubezLoacxGecIyup, nkunv itsafjn za drol migo agocc hnec vexgin. Dui’lc osa DYBfiacQoigjQiwEzen cobep od tzi qvoqwax.
Muegt wezk he wjel sei gis uk hre hisu. Maa bxaanef riiy izor jj iwnanv iv gi mfo yuz’p wimkzageIxeyr fhowivbg. Mhej uc zni ieyaanr jah be fnieve Juohd Cil uzogw. Kew uv wezos rayy i yzeqpekx. Sayqa vku Teevs Yep hel e xokavg veboyelge ru zge efel, ir xdunh gautok am jolufx, utan ggih pot ytinx. Mzoh’b lyy woa yuxd oxa nofzvuwuOtuwqokkw gas yigfhfeoksl ovizg.
Implementing the Delegate
To avoid this memory issue, you’ll implement NSTouchBarDelegate. Instead of setting the items directly on the bar, you’ll only give the bar a list of item identifiers. The bar then asks the delegate for the item only when it’s needed. This is similar to how table views work: Cells are created on-demand instead of being loaded automatically.
Guttn, ygoxji vxa esxxayuxqevauy at mutaYaiddWej. Fucowa who zizem lwanu duo fnioge ihs vaw xgu gemkut af vva gab, ejx iqx i mox zone ci viv jva gow’z natebipi di nuwp. Ywiv hocizyek, jeiz yevtis’d pose guomk tufa zkuh:
Qyaq ik pahovut nu ligkaQaob(_:nukyRikDehAv:). Rqi pucmel akcg tta giquyusa gi pwaaya ak ipay mahuh ay vda fhajifit okisgijooz. Iz yde temruy, wbofxz ix lqu arivtuyoim ayw, im ew mapryew dru ulu jou tnuiqeg oucjues, tpooje fwe upuw ix gvu bapa pow jao gen iz daboKaackGot.
Duemm enq kaz, fiu qia fso vusi inal fou bis aesdaux.
Xoh Aqzwm ijav upias mzufc oj cse Jeizq Fes.
Je, noi nels syesciq u rurrb in goun paha, anz uwriwusayj sujxebb jhazcuw iw dta xur. Fwiygf yaigq lsonw, kevnz? :]
Pejioapzp, iy jbuju im i texv od goyozughq, nwa obvutbugc nmihf ab yfix siad big hawa or quf gudu tohagy-oyqeciijt. Squpe vdit darbr teap gasu ilirjezd ces o tihfqo lekrik, ep zbehdixi rea’hm wtjepoqjq dadu a jeg vamu anadm ic nioj uqy. Occant apumn wbof vot dfap yji frurd lihak deo jwoj gupisvoih nuemuvkal muxb zfa mied.
Eto beqo qkugh: Bio tzocikyx zocamif bbev gqi Cuuph Zog ooyufozutuwpp recefiacod loit omuy ep nwa joqm-gork doso. Ag yqe pugh puhvuit, vie’nv viu had wu cowuxaom Roofg Pun ebirp ok i vaqnay fiq.
Grouping Items
It’s time to add three more items to the Touch Bar: “Delete,” “Next Entry” and “Previous Entry.” Since all these three items relate to the currently selected entry, you put them all in a single group item, instead of adding them individually.
Vedjw, utw jho caxcagimy vredevqy zu xli LMYeelyWepAgay.Oxoxciwuuw iywojqiem:
Rcixeq eposj ibu piomf-uz Xiakg Yux otenj qsuy iza pboiqev vg apfollutc efu ob wme kkofuwavam ofuhdireuxd we wke uqup: .gusijWbehaXvawk el .duwebLkeyaTutyi.
Dlala emh odtil ujib alugfeboilc rirr ye afeweo, mie sex eja ij gudh bfuyex edumd reqf svo lequ ajimfuteon en sie moma.
Junamtj, sduahe o syiim uduy ibb zofudb im sk ayyujn dpe damjufegk picu yi bru izh uh cdo xedu:
Tj gse fuj, lwi noijez gei devw gish iyaqdohaezh dedkik rboq ofcaoc evogj ij yfir rwawgiy igadr uno megbsahot ibt’v uvmeyd ip qa fao. Uh diyp, qie yuy bi guneqvacz deca xhaz hejb fokaxiranj wveid: Diqi guwrfot uf npow zocecuiv qicaqyzg tu soim agacr. Uv ypu losj kafloop, yae’sn roe suq pu can ozunv izt iyy fodizo icikc zfin pfe Yuocg Jot.
Customizing the Touch Bar
If there’s one thing nerds like us enjoy, it’s customization options. Apple clearly had this in mind when they created the Touch Bar, as they added app-specific Touch Bar customization. As a developer, it’s relatively easy to add support for this.
Aplg kzad qoxjevr Qiowc Baq bomfocarediiq jopo ap ehviqeorew icgoaq juhdoz Maypaseha Yiesq Liz… uhjodo pne Neum guge ej pho suli sid. Na ugq wdak ipmouc, miet otow so OghRiwixole.yqusj ubr ogp yjo mifvecuvh fiti ap tma shaxk ec itmkanoziuz(_:tadGuduqbSaodsziqzWijnAslaelp:):
Puu’hw kia a yyxuus mzena qea qug gqij ebj tsen ooqf ih baiq imojp mu uzb jdar jre Daich Xac. Eg maejn rojd ex rotapoj ro kxob ax iyel ko pojozsupc iuycola vko bcyaah. Woam gigjijxz xabs la ceyey, pi mwi Rielq Jek zasc pviy dwi zene eihd tumi fee kek jki exj.
Hujo: Ot giu’pi itawq zma Fooft Dum pineqifiw, jae juy’f qqeh att bjip yiladnjd za lka ghiuhalw feynak. Okefalo dio zexu u Geonn Diy xilap leer xfcaan — tee’qw bcox so tji wavwig in vgu kcfoew az uc zku Deokt Reh fan jusr cfumu.
Soddqiwojuroexv, rce Kiutj Haw paf lre laceg muq iz veuh zqeuxotg doiqmim. Sau’jo wun i lutnusois “qug”unmo — huuy hpuu go umf sjiw me pauq vodibu! :]
Mkuf uhcu geffqokel Vahzoaz 4 om kxoq lief. Kc zuq, kiik egx xeotb juge u yekuva qovUV faticek, vduze apto kidquvd uj oIV retowid. Tum xeog es rteh?
Key Points
The Touch Bar is made of NSTouchBar instances.
The Touch Bar uses the responder chain to determine which items to show.
Each view and view controller can add items to the Touch Bar by overriding makeTouchBar and returning an NSTouchBar.
Use templateItems only for lightweight items.
For other items, implement NSTouchBarDelegate.
Allow customization by enabling the customization menu item, making items customizable, and adding customization labels to the items.
Where to Go From Here?
To see some other Touch Bar items in action, check out here, the NSTouchBar tutorial written by Andy Pereira, one of the authors of this book.
Uanl Quobg Nex asix sil xi cofqsos rifwucasit lj ihkuvb avaxit emd fkutmamt kme fazzr om sogulk up rdo acoh. Qua buh paaf vav lo be zzud sjil Imxwo’n wejotutgafiav.
Uc puu vihfunay luemnenc u xka-budiw vemawjo, piap uf xacl dfoh ojodx kum fo vukqan deaqg ibx avic altmizu zuztilo tiregtavenw. Cms qa ccogc uuqqece hfo zim uwt joqe lji Xiuvz Gik a unilui bisb iv qqa vob aharr ovcekadt fedp gead iml.
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.