Im trying to make so if the user clicks on the menu icon to show the popover that the popover closes if the user clicks anywhere but popover. I set the behavior to transient but thats not doing what I thought.
Now if the user clicks somewhere on the popover bringing focus to it, then the user can click somewhere else on the screen and the popover will close. If I could force a focus to the popover I think that would fix my problem as well. Unfortunately I dont know how to do that either.
class AppDelegate: NSObject, NSApplicationDelegate {
let view : NSView!
let statusItem: NSStatusItem
let popover: NSPopover
let button : NSButton!
override init() {
statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
if let statusButton = statusItem.button {
appStatusButton = statusButton
statusButton.image = NSImage(named: "icon128off")
statusButton.alternateImage = NSImage(named: "icon128")
statusButton.action = "onPress:"
}
popover = NSPopover()
popover.animates = false
popover.contentViewController = ViewController()
popover.behavior = .Transient
}
}
Here is the view controller
class ViewController: NSViewController, WKNavigationDelegate{
var webView : WKWebView!
override func loadView() {
view = NSView()
view.translatesAutoresizingMaskIntoConstraints = false
view.addConstraint(NSLayoutConstraint(
item: view, attribute: .Width, relatedBy: .Equal,
toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 580))
view.addConstraint(NSLayoutConstraint(
item: view, attribute: .Height, relatedBy: .Equal,
toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 425))
}
}