Search code examples
xcodemacosswiftxcode7statusbar

NSStatusBar item in storyboard (OSX)


Is it possible to add a NSStatusBar item from storyboard for a desktop app? Most of the examples i saw online do everything in AppDelegate, but i don't think it's a good idea for a more complex app.


Solution

  • No, you can't add an NSStatusItem effectively in a storyboard. It has no outlets and the storyboard editor doesn't know about any of its properties.

    If you want to hang a menu off of your status bar item, you can (as of Xcode 7) create the menu in a storyboard. Drag an NSMenu into your Application Scene:

    create menu

    Connect the menu to an outlet in your app delegate:

    connect menu to outlet

    Finally, create the status item in code, using the menu loaded from the storyboard:

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        initStatusItem()
    }
    
    private var statusItem: NSStatusItem?
    
    private func initStatusItem() {
        self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)
        statusItem?.title = "Test Item"
        statusItem?.menu = self.statusItemMenu
    }