【问题标题】:NSPopover wrong position sometimes, in El CapitanNSPopover 有时位置错误,在 El Capitan
【发布时间】:2016-01-16 06:37:33
【问题描述】:

我正在构建一个小的状态栏应用程序,当我单击该图标时,它会显示一个 NSPopover。一切正常,直到我升级到 El Capitan。问题是当我第一次启动应用程序时,我会自动模拟状态栏图标按下,因为我想看到弹出框,它出现在屏幕的左下角。在我关闭它并再次打开后,它从屏幕顶部看起来很好。你看有什么不对吗?这是代码,当我在 showPopover 中打印值时没有任何问题

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())
}

另外,如果我在一小段延迟后模拟图标按下,它会起作用。

【问题讨论】:

    标签: macos swift nspopover


    【解决方案1】:

    这行得通

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

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。刚刚发现了另一个不需要延迟的解决方案。

      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.
      }
      

      【讨论】:

        猜你喜欢
        • 2016-03-12
        • 1970-01-01
        • 2015-12-31
        • 2016-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-03
        • 1970-01-01
        相关资源
        最近更新 更多