2

The toolbar in Pages (Numbers, Keynote) has a NSPopUpButton with a fixed image (irrespective of the menu that is selected). Using view debugging it turns out that this is a standard NSPopUpButton with a fixed image.

According to the NSPopUpButton docs regarding setImage:,

This method has no effect. The image displayed in a pop up button cell is taken from the selected menu item (in the case of a pop up menu) or from the first menu item (in the case of a pull-down menu).

This means that this standard NSPopUpButton has non-standard behaviour.

How could this be implemented? Because setImage: has no effect, subclass the NSPopUpButtonCell and overriding -drawImage:withFrame:inView: has no effect (because it is never called).

NSPopUpButton in Toolbar

Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62
  • I was successful with setting the Popup button from `Pop up` to `Pull down`, which is just another operation mode set in IB for example. But my button sits in the window, don't know if Toolbar shows different behavior – Volker May 15 '15 at 09:21
  • Are you saying that your popup button's image is static? I'm not sure how that is possible as it conflicts with the docs. I'm aware that to get the drop down behaviour you just need to set `Pull down`. – Daniel Farrell May 15 '15 at 09:47
  • The image then is static... at least for me. I have set it up completely in IB, assigned an image, set to pulldown and then it works for me. – Volker May 15 '15 at 09:52
  • Do you also add a menu in IB or is that loaded programmatically? This is interesting because I'm finding very different behaviour. – Daniel Farrell May 15 '15 at 10:02
  • Same question here, http://stackoverflow.com/questions/2669242/a-popup-button-with-a-static-image-cocoa-osx – Daniel Farrell May 15 '15 at 10:14
  • The NSPopUp button in my case is setup completely in IB, so image and menu as well. I don't touch it later on in code. – Volker May 15 '15 at 10:15
  • Here is what I'm seeing, https://github.com/danieljfarrell/Toolbar-with-Pull-Down-Menu. The image assigned to the popup button is attached to the first menu item?! Any ideas? – Daniel Farrell May 15 '15 at 10:32
  • Also relevant, http://stackoverflow.com/questions/23101528/nspopupbutton-of-type-pull-down-when-i-click-on-some-menu-item-title-of-popup – Daniel Farrell May 15 '15 at 10:42

1 Answers1

7

The problem here is confusion: Pull down menus display their menu's first menu item as the image/title.

Don't use -setImage: to display a static image in a -pull down menu. Instead set the first element of the menu to be the image/title that you want to display and add the selection options as additional menu items.

@Volker is absolutely correct. This is the built-in behaviour but you set the image by setting the first element in the menu not using setImage: or setTitle:.

Example, https://github.com/danieljfarrell/Toolbar-with-Pull-Down-Menu

Screenshot

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62