Cristi Băluță
Cristi Băluță

Reputation: 1305

NSPopover wrong position sometimes, in El Capitan

I'm building a little status bar app, when i click the icon it shows a NSPopover. All worked fine till i upgraded to El Capitan. The problem is when i first launch the app, i simulate the status bar icon press automatically because i want to see the popover, and it appears in the lower left corner of the screen. After i close it and open again it appears alright from the top of the screen. Do you see anything wrong? This is the code and when i print the values in showPopover there's nothing wrong

private let menu = MenuBarController()

override init() {
    super.init()

    self.menu.onMouseDown = {
        if (self.menu.iconView?.isSelected == true) {
            self.showPopover()
        } else {
            self.hidePopover()
        }
    }
}
func showPopover() {
    let icon = self.menu.iconView!
    let edge = NSRectEdge.MinY
    let rect = icon.frame
    self.popover?.showRelativeToRect(rect, ofView: icon, preferredEdge: edge);
}
func applicationDidFinishLaunching(aNotification: NSNotification) {
    let icon = self.menu.iconView!
    icon.mouseDown(NSEvent())
}

Also, if i simulate the icon press after a small delay it works.

Upvotes: 5

Views: 617

Answers (2)

Eric
Eric

Reputation: 16931

This works

func applicationDidFinishLaunching(aNotification: NSNotification) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
       popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
    }
}

Upvotes: 1

user3225395
user3225395

Reputation: 591

I had this same problem. Just discovered another solution that doesn't require a delay.

func applicationDidFinishLaunching(aNotification: NSNotification) {
    showPopover() // If you only have this, then popover will open in bottom left corner

    hidePopover() // Adding this
    showPopover() // and this will open it from the status bar button.
}

Upvotes: 0

Related Questions