Everyone must have noticed some menu items at the right corner of the system’s menu bar while working on Mac.
These are called “Menu Bar Extras”. These extra menus are typically used to display application’s or system’s status information.
Some of the built-in menu extras are time indicator, system’s volume control etc.
Although these menu items are related to “NSMenuExtra” Class but it is not recommended to use this class while creating a extra menu. This is because this class is not made public by the Apple and it’s API’s can change without any notice or information, also it is not documented for public use.
The recommended way of creating a extra menu is by using “NSStatusItem” Class which is the super class of “NSMenuExtra” Class. Here is how we can create a “Menu Extra”
1. In the .h file create a object of NSStatusItem Class.
2. Create an IBOutlet for an object of NSMenu Class.
@interface MenuExtraAppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
3. In Interface Builder add a Menu (NSMenu) and connect it’s reference to the _menu of MenuExtraAppDelegate Class.
4. Create .m class and write a function “createMenuExtra” to create the menu at run time.
5. The menu thus created will be available for the lifetime of the application and can be accessed even if the application is not active.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
// Insert code here to initialize your application
// Create a NSImage from the image MenuIcon present in bundle.
NSImage*icon = [NSImage imageNamed:@"MenuIcon.jpeg"] ;
// Alloc Memory to the NSStatusItem's Object using NSStatusBar Class's API.
_extraMenu = [[NSStatusBar systemStatusBar]statusItemWithLength:
//Retaining the _extraMenu is very important for the display of Menu item on the System's menu bar.
[_extraMenu retain] ;
[_extraMenu setImage:icon] ;
[_extraMenu setHighlightMode:YES] ;
//Set menu item to be displayed on the click on MenuExtra(_extraMenu)
[_extraMenu setMenu:_menu] ;