Update NSTouchBar on the fly to add/remove items programmatically

巧了我就是萌 提交于 2019-12-05 12:10:58

Four ideas:

  1. Try changing your touch bar's defaultItemIdentifiers to the set of item identifiers that should be shown. Note that this would be problematic if the user has customized the touch bar, but I think swapping items on-demand and customizing the touch bar doesn't go well together anyway. This also has the advantage that you don't need to return nil in touchBar(:makeItemForIdentifier:).
  2. Calling makeTouchBar() will create a new NSTouchBar instance, but not change the touchBar property. Try something like

    viewController.touchBar = viewController.makeTouchBar()
    

    or

    viewController.touchBar = nil
    
    1. Set the touchBar property on the NSTableRowView that should show extra items when selected, and make sure to include the otherItemsProxy in your defaultItemIdentifiers. As the contents of the touch bar are comprised of all elements in the responder chain, this might include the touchBar property of the table row (provided it can become first responder).

    2. Are you sure that these items should be hidden when the row is not selected? Consider disabling them instead (e.g. by setting the enabled property of the buttons they contain to false).

Just invalidate the touchbar in your view controller:

self.touchbar = nil

The delegate method makeTouchBar() will then automatically be called. Using your flags, you can easily choose the icons to appear.

EDIT: this solution has been tested and works fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!