CustomView Does not show up in StatusBar

1.2k Views Asked by At

I am trying to make a SystemStatusBar popover for Mac. (Roughly, translating this Cocoa app to a Swift app). However, the view that I am using never shows up and the popup appears in the bottom left of the screen replicating the StatusBarItem.

This is what I expect (and that happens in the example from case of example from the link): enter image description here

and this is what actually shows up (in my, Swift version of the application) instead of the NSPopover being shown in the StatusBar [Showing or Hiding the popup is controlled by the two buttons as shown in the previous figure. In this screenshot I have not added that window as it remains the same.]:

enter image description here

This is the AppDelegate:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    var statusView : StatusView!
    var popController: PopViewController!


    @IBAction func showPop(sender: NSButton)
    {
        statusView.showPopup()
    }

    @IBAction func hidePop(sender: NSButton)
    {
        statusView.hidePopup()
    }

    func applicationDidFinishLaunching(aNotification: NSNotification)
    {
        var height = NSStatusBar.systemStatusBar().thickness
        statusView = StatusView(frame: NSMakeRect(0, 0, CGFloat(height), CGFloat(height)))
    }
}

The CustomView:

class StatusView : NSView, NSMenuDelegate
{
    var imageView: NSImageView!
    var statusItem: NSStatusItem!

    var popover: NSPopover!
    var popController: PopViewController!

    required init? (coder: NSCoder)
    {
        super.init(coder: coder)
    }
    override init(frame frameRect: NSRect)
    {
        var height = NSStatusBar.systemStatusBar().thickness
        imageView = NSImageView(frame: NSMakeRect(0, 0, CGFloat(height), CGFloat(height)))
        statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(CGFloat(height))

        super.init(frame: frameRect)

        imageView.image = NSImage(named: "mf-image-black.png")
        self.addSubview(imageView)
        statusItem.view = self

        popover = NSPopover()
        popController = PopViewController(nibName: "PopViewController", bundle: nil)
        popController.view = self
        popover.contentViewController = popController

    }

    func showPopup()
    {
        if(!popover.shown)
        {
            popover.showRelativeToRect(self.frame, ofView: self, preferredEdge: NSMinYEdge)
        }
    }

    func hidePopup()
    {
        if(popover.shown)
        {
            popover.close()
        }
    }
}

and the ViewController:

class PopViewController: NSViewController
{
    @IBOutlet var statusView: StatusView!

    override init?(nibName: String?, bundle: NSBundle?) {
        super.init(nibName: nibName, bundle: bundle)
    }

    required init?(coder: NSCoder)
    {
        super.init(coder: coder)
    }
}

I am not exactly sure what is that I am missing here. The StatusItem never seems to make use of the PopViewController nib.

0

There are 0 best solutions below