繁体   English   中英

失去焦点时NSPanel不会隐藏

[英]NSPanel not hiding when focus is lost

我正在尝试创建像 Spotlight 一样的 window。 与 Spotlight 中一样,它应该在单击背景时隐藏。 我尝试使用 NSWindow 未成功,但我被引导相信使用 NSPanel 可以解决问题。 但是,即使使用 NSPanel,window 也不会隐藏。 这是我正在使用的代码。

let panel = NSPanel(contentRect: CGRect(x: 0, y: 0, width: 200, height: 200), styleMask: [.titled, .nonactivatingPanel], backing: .buffered, defer: true)
panel.level = .mainMenu
panel.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
panel.orderFrontRegardless()

这是由于使用了 window 级别( .mainMenu高于所有窗口),因此您需要通过委托方法显式隐藏它

因此,假设您在 controller 中创建窗口/面板,请将 controller 设为 window 的代表

panel.delegate = self

并实现类似的东西

extension ViewController { // << your controller class here
   func windowDidResignKey(_ notification: Notification) {
       if let panel = notification.object as? NSWindow {
          panel.close()
       }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM