Popup button with static image (Cocoa OSX)

I am trying to create a popup that always displays + as an image, and when you click on it, a context menu appears that will allow you to decide what type of object you want to add. Is there a way to do this using NSPopupButton? I saw in the specs for NSPopupButotn that the SetImage method has no effect, so it seems that it is unlikely to work using this class. It's right?

+2


a source to share


4 answers


Yes, the visible appearance is NSPopUpButton

not correct for what you want.

What you want is regular NSButton

with the attached menu. In Interface Builder, clear the title, set the border to the square, position to the center icon version, and the image to NSAddTemplate

.

Then create NSPopUpButton

, set it to "Move Down", hide and align / resize it to the bottom of the NSButton.



Finally, connect performClick:

from NSButton

to performClick:

to NSPopUpButton

.

This will not handle a button click on a button; for this you will have to write code to use (not draw) NSPopUpButtonCell

.

0


a source


Why not just use NSPopupButton? Using two buttons for the same task does not seem right as it is mentioned that it does not behave normally if the button is held down.



To make the popup menu work the way you like ... set it as the dropdown type. Do not provide the popup button itself with any name or image. If there is no name or image in the popup button, the first menu item becomes the button's title. Menu items can have images, so don't give the first menu item any title, just set its image and the image you see on the popup button. For the rest of the menu items, just add them as usual after the first menu item. Note, if you change menu items programmatically in code, just make sure you leave the first menu item tactfully and you should be fine. I am doing this in several of my applications with no problem.

+8


a source


The key is to use NSPopUpButtonCell's

setUsesItemFromMenu

:. Apple's doc has an example on how to use this.

+4


a source


The pop-up button displays the image and name of the first menu item in its associated menu. As such, it is best to include the image you want to display as the popup button as the first menu item and then hide it.

If you haven't figured it out yet, skip to the next video once, it will definitely make you understand.

This tutorial shows how to create a toolbar item (NSToolbarItem) with a dropdown menu for the NSToolbar in Interface Builder: Link

0


a source







All Articles