Daniel Farrell
Daniel Farrell

Reputation: 9750

NSPopUpButton in a toolbar with fixed image similar to Pages.app UI

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

Upvotes: 2

Views: 934

Answers (1)

Daniel Farrell
Daniel Farrell

Reputation: 9750

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

Upvotes: 7

Related Questions