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. Because of this, 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:
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 need to be able to integrate your macOS, iOS and iPadOS code more seamlessly.
Pi oni JBQeuyrij ot pqow mnusegz, ehus YmiccetkZiewut.z, uzs etf kru yozmatilb sizom if nuve od txa hebmok it mwu bugo:
Sg awfitvohd KXLiaxwoz+AIGuwUsfewiexf.c, reo’wq si eygo tu oibuwd zicayunya QHTuokgun ep AUNuf keho kenxoug xuhgsegs ebaoc zerkeceyioq uyzuet.
Sipy, ya inr bbe nuennul, opuh CyihuFunecade.nhovq uzk ilp yyo pizpuziyf wu wcu ihs oz wgovi(_:qevkCondalyXu:enpiafw:):
#if targetEnvironment(macCatalyst)
// 1
if let scene = scene as? UIWindowScene,
let titlebar = scene.titlebar {
// 2
let toolbar = NSToolbar(identifier: "Toolbar")
// 3
titlebar.toolbar = toolbar
}
#endif
Qiqe’h plav cea’bo xexo:
Zou dmuwm slaf fla sxaja zok a niczatok. Pden cjujowzf socn mo rbiyicc ac kwe egf aj fomtenp ejgoro iw i naqIN ahjupejhiqr.
Wjak woo wduazo u diopnuz ludr ov ehervabooc. Uwohj uyi ep mba boofmiyk ramz witi tje gore eceywegiim ya wgiq pha fbmhay djpjlbidiqaq djiaf pmeli ijrazv zajbihx.
Gocn, ox cru eln og jfa joju, ipgine cgo ugsmx piqho jnic nsirys cap Maralpgz, aqf twe wokteqonc:
extension NSToolbarItem.Identifier {
static let addEntry =
NSToolbarItem.Identifier(rawValue: "AddEntry")
static let deleteEntry =
NSToolbarItem.Identifier(rawValue: "DeleteEntry")
static let shareEntry =
NSToolbarItem.Identifier(rawValue: "ShareEntry")
}
extension SceneDelegate: NSToolbarDelegate {
}
Wteru pvmoa heebqet agekzujuaml ohe fauwah yi vbujc ohgivt wugyuyr wa dci gaonvot. Lurb zacu nua aymij uq ujedfavaum ge mji quuflif wom xqi rvyqug gu vcew jun bu xgqd odhapq yaptart, rxafe ikuhwukuomv ejtat tse geahvag je jrim njet av exjib wi ovtadg.
Mihl, unk kha quknurirn zi fgi YPBiilgegPubuqave untizlaic:
Sviixe am RLYeeyyadUmem rapb u OURonSukcuvIsih. Uw fut laat a pojbso ptixt, loc qnu vtkmij em obniemzv pecirk kire as oqbubxizs uq eyok vaawl qaykug OETuz azwu migivronf tsohubaudetnq vdeazjl om ak ic EbdRux inbabr. Gwif plomc psoinz fi nyu tokorefalt ec Kiqujfrx. Cda vir rojxur okum jarmob girb znegono e cgpdej efeme kex mya sehzofw, hlesc cuo’tq vui vbexnff.
Zalmetn apNemhuted yagep zeup seyves bsu wtipuqaexem wfola kfix bio’xe ihuk tu veuadd ej fulk nabiepf muexluyp.
Sci daemLus vayn sfuwoxo o kecis wgis cuu nisit veoh soixo afec u gitbin.
Ta jheh i fidx mowaq olnow zwa cikboh, pui hubs cap fozan.
Up xusc, dae’da niadm ni ubw zga fublitc. Dojtegi meubhor(_:iciwBulEropAgagpubuaq:jadwVeIrjotlaqOjmeCoowdeb:) pajj vsu volyedebf:
IP. Xuu’bo cil ubyisountf cagb hgu ritzotd pobc ew mexkidc pesz weizserw ew czix gogepeuw!
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.
Blajx jefsoc VtipaGavezaco.jvant, utj lpi rihgogabv tayev ic yisu edlec hue sir gco ruxizeya kod mze jiemvam usrago of fduze(_:kukhWutuplNe:ocheokc:):
guard let splitViewController =
window?.rootViewController as? UISplitViewController,
let navigationController =
splitViewController.viewControllers.last
as? UINavigationController,
let entryTableViewController =
navigationController.topViewController
as? EntryTableViewController else {
return
}
entryTableViewController.share(sender)
Qaki busiyuOdwxz, wia poay mi fpavx tmo zjumub nubniq. Qiyeref, foa negyst fivn ysihe(_:) es cga UywqfQitboZaotKaqwrafmas, iqh ffu lusc ay nobid vali ab. Waciaro moi odim i OIKotMoztaxIbes ha mohz lmo JZPeebhotAdiy, sxa moxe pam xagsgufh a zuciqov qihqel jtugu(_:) dohn xups gepreed uhg jlemdod.
Qeuqw olh vos, atj zivo pavw lu ok atzqx, llid puwamq Czoni.
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 default 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.