【问题标题】:NSPanel not hiding when focus is lost失去焦点时NSPanel不会隐藏
【发布时间】:2020-12-23 09:21:51
【问题描述】:

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

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

【问题讨论】:

    标签: swift nswindow nspanel


    【解决方案1】:

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

    所以假设您在控制器中创建窗口/面板,让该控制器成为窗口的代表

    panel.delegate = self
    

    并实现类似

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

    【讨论】:

      猜你喜欢
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      相关资源
      最近更新 更多