It’s been on the Mac for a long time, and I don’t know if you’re interested in apps that have no form, don’t display on the Dock, and only display on the StatusBar.
For example, Apple’s own Bluetooth and Wifi are actually an APP, as well as some apps in the APP market (I will not name the specific ones, for fear of promotion suspicion), which can not be displayed on the Dock and have no forms. Even the menu that pops up when you click on the StatusBar still allows you to customize the view.
So today, I’m here to guide you step by step how to create such an APP
The demo code has been uploaded to GitHubportal
To create the APP
Open Xcode, here we select MacOSApp whose language is Swift and interface is Storyboard:
Set not to display on Dock
Open the plist file in the project, add the property Application is Agent (UIElement) and set it to YES. Similarly, if you want to display it on the Dock later, just set it to NO:
Edit Menu, eliminate all Menu bars that are not needed
Open up the Storyboard, select Menu, and delete all the extra menus. That leaves StatusBarAppExample:
Don’t show the Window
Open the Storyboard again and delete the Window Controller Scene from the project:
In this way, we basically achieve an App that does not display on the Dock, and there is no Window.
But after running, we found that the program was running normally, but the StatusBar did not show our App. Don’t panic! Keep reading
Add icon for StatusBar
- Firstly, two icon pictures of different sizes should be prepared, respectively
1x
and2x
And the magnitude is18x18
and36 * 36
Then drag it to Assert:
- Set up the
Menu
theIBOutlet
toAppDelegate
It’s in the file. It’s in the fileStoryboard
Right clickmenu
Then drag it to the AppDelegate to generate it automatically. (If you don’t understand all of this, I suggest you take a look at Objectivec from Start to Quit.)
- Then, in
AppDelegate
Declare aNSStatusItem
Objects: as follows:
var statusItem: NSStatusItem?
Copy the code
- rewrite
AppDelegate
In theawakeFromNib
This function.
override func awakeFromNib() { super.awakeFromNib() statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) statusItem? .button? .title = "WindowSnap" let itemImage = NSImage(named: "SplitScreen") itemImage? .isTemplate = true statusItem? .button? .image = itemImage if let menu = menu { statusItem? .menu = menu } }Copy the code
The full AppDelegate looks like this:
When you run the App again, you should see our App on the StatusBar:
This basically completes an App that has no Windows, doesn’t display on the Dock, and only displays on the StatusBar.
Wait, isn’t it possible to customize views? Is it like taobao’s Mac version of Wangwang? Don’t worry! You can read my next article on MacOS Development Series 6- Customizing menu bars NSMenu and NSMenuItem.