Show folders in NSPopUpButton

I want to show icon folders in my NSPopUpButton. This is common for pop-ups, which are used to select a file path. I am a new user and I cannot send images. U can see this, for example, in the U directories Torrent-> prefer-->

Please give a detailed answer because I am completely new.

Thank you very much and sorry for my bad english

+4
source share
1 answer

For each NSMenuItem in your menu, you need to set the corresponding image by calling setImage:

In short, you need to prepare your menu item, attach it to the menu and attach it to your pop-up button, for example:

 NSPopUpButton *yourButton = [[NSPopUpButton alloc] init]; NSMenu *yourMenu = [[NSMenu alloc] init]; NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"some label" action:nil keyEquivalent:@""]; NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFile:@"yourFilePath"]; [iconImage setSize:NSMakeSize(16,16)]; [menuItem setImage:iconImage]; [yourMenu insertItem:menuItem atIndex:0]; [yourButton setMenu:yourMenu]; 

Pay attention to the use of iconForFile: in NSWorkspace , which allows you to display the same icon as in Finder.

For more examples, you can see this sample Apple code: ButtonMadness

+4
source

Source: https://habr.com/ru/post/1447161/


All Articles