Show NSPopover from NSToolbarItem Button

后端 未结 4 987
孤城傲影
孤城傲影 2021-02-08 12:50

I want to show an NSPopover from an NSToolbarItem button in my toolbar.
(i.e. positioned below the button).

Ideally, I want to pass the

4条回答
  •  悲哀的现实
    2021-02-08 13:20

    Instead of getting the view from the IBAction sender, connect an IBOutlet directly to the toolbar item and use that to get the relative view:

    In your header file:

    @property (weak) IBOutlet NSToolbarItem *theToolbarItem;
    @property (weak) IBOutlet NSPopover *thePopover;
    

    In your implementation file, to show the popover:

    [self.thePopover showRelativeToRect:[[self.theToolbarItem view] bounds] ofView:[self.theToolbarItem view] preferredEdge:NSMinYEdge];
    

    This will also work for showing popups from menu item selections inside a toolbar item.

提交回复
热议问题