Reputation: 4245
I wanted to create an application that mainly only involves the status bar. So far I have created the status bar item using NSMenu
and NSStatusBar
and I have also removed the dock icon with this bit of code on load:
[NSApp setActivationPolicy: NSApplicationActivationPolicyAccessory];
But what I still have is the NSWindow
appearing when opening the app.
How can I prevent this? I also would like to be able to re-open it.
I have come up with a horrible way to close it:
[_mainView setFrame:CGRectMake(0, 0, 0, 0)];
Where _mainView
is the main NSView
in my viewcontroller that is conected to the nswindow
.
I then want to be able to open the window again but with this time a table. But I get the error:
<Warning>: void CGSUpdateManager::log() const: conn 0x18de3 token 0x31fffffffffdafd
When resetting the frame back to the original size.
Also if I close the NSView that then means I can't re-open the view again.
This is a long winded explanation of an application that can control whether the window the viewcontroller is in, is opened or close.
Upvotes: 4
Views: 1125
Reputation: 6932
To make a NSStatusBar item app that only shows in the status bar and not in the Dock or Application Tabbing. And not show any of the normal menus. i.e file,edit,view and so on..
You need to add the Application is agent (UIElement) - (Boolean) YES key - value to the application info.plist.
And also make sure that the windows 'visible At Launch' is switch off in the attribute inspector.
Update:
In a none storyboard application (OS X)
Setting the 'visible At Launch' to off in IB for a window, will stop the window appearing at launch.
But with a storyboard application. This will not work.
The 'visible At Launch' is already set to be off. But regardless of that, the window will always show.
(I think this is part of the design of storyboards and Human interface guidelines by apple. Maybe because they stem from iOS and there should always be a window present.)
There are possibly a few ways of changing this behaviour but I found that if you uncheck the initial Controller in the Attributes Inspector for the NSWindowController
This will stop the window showing up at launch. Which makes sense since the app now does not have any instructions to show anything initially.
To open the window you can simply link a menu item to the NSWindowController's Presenting Segue Show: method in IB.
If you want to open the window programmatically, then you have to re point to the controller in the code.
Now go to your AppDelegate.h file and add a the Property and IBAction:
@property (strong) IBOutlet NSWindowController *winController;
-(IBAction)showWindow:(id)sender;
Then go to the AppDelegate.m file and add this code in the applicationDidFinishLaunching
NSStoryboard *storyBoard = [NSStoryboard storyboardWithName:@"Main" bundle:nil];
NSWindowController * main = [storyBoard instantiateControllerWithIdentifier:@"Main"];
_winController = main;
(note just adding the controller by linking it directly with a property in the AppDelegate did not work for me)
Now add the IBAction code to the AppDelegate.m
-(IBAction)showWindow:(id)sender {
[_winController showWindow:self];
}
You will need to link the IBAction as normal to which ever menu item you want to open the window via IB.
Upvotes: 3