Update Nstouchbar on the Fly to Add/Remove Items Programmatically

Update NSTouchBar on the fly to add/remove items programmatically

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).

Hide NSTouchBar button

I found the answer!

Just write:

<button_name>.isHidden = true

or if you want to disable and not hide:

<button_name>.isEnabled = false

In my case I had button that called "submitButton" so I wrote:

submitButton.isHidden = true

and when I wanted it to appear I wrote:

submitButton.isHidden = false

Knowing when NSPopoverTouchBarItem will show its collapsed view

The docs were not too clear, but subclassing NSPopoverTouchBarItem gives you -(void)showPopover:(id)sender and -(void)dismissPopover:(id)sender.

You can then define a delegate method to tell the parent class that this popover did show.

-(void)showPopover:(id)sender {
[super showPopover:sender];
[self.delegate touchPopoverDidShow];
}


Related Topics



Leave a reply



Submit