Macos Menubar Application: Main Menu Not Being Displayed

macOS menubar application: main menu not being displayed

This is my workaround solution for now:

As I wrote in the question, if I click on another app and come back to my app, the menubar is being displayed. I am simulating this when I try to show the preference window:

    NSApp.setActivationPolicy(.regular)
NSApp.activate(ignoringOtherApps: true)
window.makeKeyAndOrderFront(nil)

if (NSRunningApplication.runningApplications(withBundleIdentifier: "com.apple.dock").first?.activate(options: []))! {
let deadlineTime = DispatchTime.now() + .milliseconds(200)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
NSApp.setActivationPolicy(.regular)
NSApp.activate(ignoringOtherApps: true)
}
}

This is not a perfect solution. If I don't find a better solution, I will file a bug.

OS X application menu bar not showing correct title

Open the xib or storyboard file containing the menu, and select the menu item(s) you wish to change. You may set the menu item titles as desired via the Inspector:
Sample Image

How to display application icon in menubar even application is quit

You can definitely switch an app to background (Accessory) mode and back again. Semantically, the application never quits.

The basic idea is to use the NSApplicationDelegate protocol to switch back and forth between accessory and regular app modes. There are already methods to cancel quit, catch all windows being closed, and to handle the user trying to launch your app even if it's still running. So put it all together, and you get the code below.

I left in code here showing how to load and unload the main GUI controlled by the NSWindowController self.wincon where self is the application delegate object. It loads and controls a separate MainWindow.xib. If you don't have a window other than the mainmenu, it might be unnecessary.

I also have a user preference that needs to be set to enable all of this behavior. By default, it will really, really quit.

I have nothing in MainMenu.xib but the menu-- switching to accessory mode will mean the menu is not displayed.

// Helper to close main window and switch to accessory mode
- (void) switchToBackgroundMode
{
@autoreleasepool {
// Need to check loaded to prevent closing a closed window and
// triggering a second call to applicationShouldTerminateAfterLastWindowClosed
if ([self.wincon isWindowLoaded]) [self.wincon close];
self.wincon = nil;
}

// Hide the menu and dock icon
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
}

#pragma mark Application Delegate Methods

// Called with a CMD-Q
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
// Cancel terminate if pref set
if ([MyPreferencesController runInBackground])
{
[self switchToBackgroundMode];
return NSTerminateCancel;
}
return NSTerminateNow;
}

// Called when all windows closed
- (BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
if ([MYPreferencesController runInBackground]) {
// This check is necessary to avoid calling switchToBGmode twice on a quit
if (![NSApp activationPolicy] == NSApplicationActivationPolicyAccessory)
[self switchToBackgroundMode];
return NO;
} else {
return YES;
}
}

// Called if the app is in accessory mode and the user activates it through the dock or by
// clicking a userNotification or trying to open the app
- (BOOL) applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag
{
if (!self.wincon) {
self.wincon = [[MYMainWindowController alloc] initWithWindowNibName:@"MainWindow"];
}

// This ensures that the dock icon comes back
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];

// Show the window
[self.wincon showWindow:NSApp];
[self.wincon.window makeKeyAndOrderFront:NSApp];
return YES;
}

Notes added 10/6/2016 since this has gotten some traction:

There is an older answer to this question. It has a good discussion of the history of the changes but lacks sample code.

Finally, this answer and question entirely lacked the LSUIElement keyword, which was a historical OSX plist setting for apps of this type. As described in the answer above and this more recent question, LSUIElement should be considered deprecated. If you have found an old blog post mentioning it, hopefully you have found more recent code samples that recommend not using it at all.

Keyboard shortcuts not working in Menubar Application for macOS

It looks like you are using NSStatusItem.

The operating system passes key events that it doesn't handle to the front application, which compares them to any key equivalents in its main menu and current window. They are not compared to items in the status bar, and since the status bar is actually a different application, it won't matter that your application is in the foreground. When you have the menu open, it is the window in focus. This is why it works in those circumstances.

I'm answer this from another post. Please refer that post for more detail.



Related Topics



Leave a reply



Submit