0

I need to remove some items from right-click context menu, but the toolbar does not contain a public API to access 'toolbarView'.

Is it possible to customize the menu without using private API?

Elden
  • 636
  • 1
  • 6
  • 21

1 Answers1

1

You can access and modify a toolbar contextual menu when the toolbar is created, i.e. in -[awakeFromNib]:

- (NSMenu *)toolbarMenuInWindow:(NSWindow *)window
{
    NSView *contentView = window.contentView;
    NSView *toolbarView = contentView.superview.subviews.lastObject;
    NSMenu *toolbarMenu = toolbarView.menu;
    return toolbarMenu;
}

Now you can directly edit menu items and hide or disable them.

Vadim
  • 9,383
  • 7
  • 36
  • 58
  • In my current solution, I tried to subclassing the NSWindow and overriding the 'mouseDown:' and 'rightMouseDown:' method. It works 'safely'. – Elden May 10 '12 at 14:47
  • 2
    Adding a note that this method does not work in more recent OS versions, where toolbarView.menu will be nil. Instead, get the menu from the contentView.superview, i.e., contentView.superview.menu. Reference from apple dev forums: https://developer.apple.com/forums/thread/21887 – Corbell Oct 25 '20 at 03:43