Search code examples
macosswiftappkit

NSButton won't let me set imagePosition property


I'm trying to build a menubar application in swift. I want to set an image and text for the status item. Evidently NSStatusItem has deprecated setting a custom view since 10.10, which is fine, since I am able to set an image and text on the status item's button. However, I'm unable to set the imagePosition property for some reason and so the text and the image overlap.

This is my code:

let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)

  func applicationDidFinishLaunching(aNotification: NSNotification) {
    let icon = NSImage(named: "statusIcon")
    icon!.setTemplate(true) // best for dark mode

    statusItem.button!.image = icon
    statusItem.button!.imagePosition = ImageLeft
    statusItem.button!.title = "Hello, world"

    statusItem.menu = menu;
  }

The problem is that Xcode gives me an error on this line:

statusItem.button!.imagePosition = ImageLeft

It says "Use of unresolved identifier 'ImageLeft'", but from what I can tell from the documentation (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSCell_Class/index.html#//apple_ref/c/tdef/NSCellImagePosition) that is the identifier I would want to use.

Can anyone help?


Solution

  • I figured it out. Evidently I have much to learn about Swift syntax.

    This line allows it to work:

    statusItem.button!.imagePosition = NSCellImagePosition.ImageLeft