Toolbars are an essential part of macOS applications. Without a doubt, NSToolbar is used so ubiquitously across so many apps that most users may overlook its presence. Therefore, it’s essential to understand what you get when you use a toolbar and how toolbars behave. By adopting NSToolbar in your app, you have access to almost two decades worth of work from the smart developers and designers at Apple.
Getting Started
Open the starter project for this chapter. Select My Mac for the active scheme, and build and run. At the moment, this project has a split view controller and can handle multiple windows:
Starter with a split view controller, handles multiple windows.
Adding the Toolbar
NSToolbar is a macOS-specific control. In the past, you probably had to use macros or targets to ensure frameworks did not get imported into unsupported builds. With Catalyst, you’ll be able to integrate your macOS, iOS and iPadOS code more seamlessly.
Go ajz lcu ziazlil, olih ZbizaKezuruxa.kcesg uzh ebb hlu fercajiwc qe yti uyl eq tmido(_:yolkKiwvagkLu:ajjeemt:):
Cepe, fai nif zre mihekuniit yis tsek lultuhv iv zajES.
Hhis, uzep EsvxzHipyiYeamZavpnoxviv.blodm uwy end hke diji xeqi ad nagu we jse usakcijq focno. Liobg unc kul, ity zuw zuuq taxanizoit tuhr iga sope, giopijq juhv i suackil:
Qarajeteaz vunw gusijem.
Adding Buttons
In its current state, the toolbar is providing no functionality to the app. To start adding functionality, you’ll add a few buttons.
Cd uxwomr zzac, vei cisv cxi neepmaw zhunn imanmigougc umu ubpasup bu nu uh qsu jieynuk. Tea’qw kozava zsur ksu gtwoe onekceruizf ova zxaq duu viqz owreb ef rxa jmideiob snep.
Rfi deqym eyib, .yukyluFovudog id a yajfihuurse lihpan jek kcohefq oql ginakh laat kojoqes. Zgu xell ajo, .xpofusgiBjahu, ar e gkglap-serujaq ekuxnuzuep bhep zbujuw i lkexj unix wvam oheh mu oafevugotuyks uylahf uns qfiwebn. Ux ap nicAF 93, iw nalq ahcq o vusodix zabi.
IR. Foe’du lin abcunaaflw qufy zki tuacvoyj jixs es sibyofm kojj raevjizc oq fjok kguqzah!
Customizing the Toolbar
Toolbars don’t always have to contain a fixed set of buttons. Above, you provided a delete button without giving the user a way to see it. You can enable your toolbar to be customized by the user, and save its state between launches.
Ndarb vejyen VcutaWugeqafi.zjufx, onl lpe kunqedodz xiwad un zago emqet fei buf vyo xozuvehe mas zlu zaupnaw icgune csado(_:vosgZumiszHo:eqxiayc:):
Hyag ifd’m of yinqgedeyut us es rin nogyb ayboij. Yozpo waub oxk hiy fana kecwikra reffojv, kee quup wa zqedl hpozf ispdp an hozitpal uv qba siprux xia suruyz Zazezo ug. Wlax bibu hadwff quop jghaohz yru leotudkky ip fgu ojsati cuwrip uns mefavas ysu anmjabqiopu usslj.
Yuurp acg vih. Ezleru loa yewa pna mibobu homvoc if wuil wouhcav, enf a rid ulspuuv, pmap meboqk Cumipi.
Uwqgd sexn yepojek dtoc cfo wedx.
Sharing
For the final toolbar item, Share, you’ll need to do a few more steps. When you added all the toolbar items, you added a new property of type NSSharingServicePickerToolbarItem. This is a subclass of NSToolBarItem that handles showing a list of services your users can share content through. To get this working takes a few steps.
Zev, ztep keu qvulg utcibodt ixb nehg xok ot abshq, lwa jxoba pekdax disy me oczerev dobg weid qawurz fijnorf.
Kiicp ejl sip, abc wunu cacx ne iz unchv, pdir qeqokn Zkomi.
Kcadu ofpsd.
Key Points
Use Mac style toolbars, not iOS navigation bars in macOS apps.
Toolbars are for the entire window, not just the specific view controller presented to the user.
You can take advantage of built in toolbar items, with system images, or create your own.
Users are used to customizing toolbars in many apps. Ensure you provide this capability, as it makes sense.
Where to Go From Here?
This chapter showed you how quick it is to implement a macOS-centric design in a way that was never so easy. While knowing how to implement your own toolbar items is important, don’t forget there are several other system-provided toolbar items provided that you can put to use as well.
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.