Ultimate Catalyst Guide

How do I nest touchbars?

released Fri, 05 Jul 2019
Swift Version 5.1

If you have multiple view controllers in your responder chain that each want to insert something in your NSTouchBar you can simply define the outermost (highest priority) touchbar to host other touchbars alongside. You do that by adding the system default NSTouchBarItem.Identifier.otherItemsProxy to your list of defaultItemIdentifiers:

     override func makeTouchBar() -> NSTouchBar? {

         let touchBar = NSTouchBar()

         touchBar.delegate = self

         touchBar.defaultItemIdentifiers = [MyButtonTouchBarIdentifier,

                                            NSTouchBarItem.Identifier.otherItemsProxy]

         return touchBar

     }

Now, if, say, a child view controller would also implement makeTouchBar, its touchbar would be displayed next to our MyButtonTouchBarIdentifier button.