在 Swift 4 中创建类似 Spotlight 的窗口? [英] Create Spotlight-like window in Swift 4?

查看:31
本文介绍了在 Swift 4 中创建类似 Spotlight 的窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个窗口作为我的应用程序的一部分,该窗口在点击某个按键后显示一个类似 Spotlight 的窗口.

I want to create a window as part of my app which is displaying a Spotlight-like window after hitting a certain keystroke.

但我得到的只是隐藏title:

override func viewWillAppear() {
    self.view.window?.titleVisibility = .hidden
    self.view.window?.titlebarAppearsTransparent = true
    self.view.window?.styleMask.insert(.fullSizeContentView)
}

结果:

如何使用 Xcode 10 和 Swift 创建这样的视图?

How can I create such a view with Xcode 10 and Swift?

推荐答案

您可以使用 NSPanel 创建自定义窗口

You can create a custom window using NSPanel

final class Panel: NSPanel {
    init(contentRect: NSRect, backing: NSWindow.BackingStoreType, defer flag: Bool) {
        super.init(contentRect: contentRect, styleMask: [.titled, .resizable, .closable, .fullSizeContentView], backing: backing, defer: flag)
        
        self.isFloatingPanel = true
        self.level = .floating
        self.collectionBehavior.insert(.fullScreenAuxiliary)
        self.titleVisibility = .hidden
        self.titlebarAppearsTransparent = true
        self.isMovableByWindowBackground = true
        self.isReleasedWhenClosed = false
        self.standardWindowButton(.closeButton)?.isHidden = true
        self.standardWindowButton(.miniaturizeButton)?.isHidden = true
        self.standardWindowButton(.zoomButton)?.isHidden = true
    }
    
    // `canBecomeKey` and `canBecomeMain` are required so that text inputs inside the panel can receive focus
    override var canBecomeKey: Bool {
        return true
    }
    
    override var canBecomeMain: Bool {
        return true
    }
}

并在 AppDelegate 中像这样使用它:

and use it in AppDelegate like this:

final class AppDelegate: NSObject, NSApplicationDelegate {
   lazy var panel: NSPanel = FloatingPanel(
        contentRect: NSRect(x: 0, y: 0, width: 700, height: 320),
        backing: .buffered,
        defer: false
    )

   func applicationDidFinishLaunching(_ aNotification: Notification) {
        // panel.contentView = ...
        panel.makeKeyAndOrderFront(nil)
        panel.center()
   }
}

这篇关于在 Swift 4 中创建类似 Spotlight 的窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆