首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用menubar按钮重新打开macos应用程序

如何使用menubar按钮重新打开macos应用程序
EN

Stack Overflow用户
提问于 2022-07-11 09:56:01
回答 1查看 43关注 0票数 0

我用快速和故事板创建了一个带有菜单按钮的应用程序,当我点击关闭按钮时,窗口关闭了,菜单按钮仍然在菜单上,这很好。接下来我要做的是通过单击菜单栏按钮重新打开窗口。在搜索之后,我发现我可以使用这段代码将它带到前面,但是只有当窗口仍然打开时,它才能工作。当它关闭或小型化时,我怎样才能把它带回来?

代码语言:javascript
复制
NSApplication.shared.activate(ignoringOtherApps: true)

这就是appDelegate

代码语言:javascript
复制
class AppDelegate: NSObject, NSApplicationDelegate {

private var statusItem: NSStatusItem!

func applicationDidFinishLaunching(_ aNotification: Notification) {
    // Insert code here to initialize your application
    
    statusItem = NSStatusBar.system.statusItem(withLength: 16.0)
    if let button = statusItem.button {
        button.image = NSImage(named: "remote-control")
        button.image?.size = NSSize(width: 16.0, height: 16.0)
        button.image?.isTemplate = true

        button.action = #selector(bringToFront(sender:))
    }
}

func applicationWillTerminate(_ aNotification: Notification) {
    // Insert code here to tear down your application
    print("terminate")
}

func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
    return true
}

@objc func bringToFront(sender: AnyObject?) {
    NSApplication.shared.activate(ignoringOtherApps: true)
    NSApp.windows.last?.makeKeyAndOrderFront(nil)
}}

这是窗口控制器

代码语言:javascript
复制
class MainWindowController: NSWindowController {

override func windowDidLoad() {
    super.windowDidLoad()

    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    
    window?.title = ""
    let styleMask: NSWindow.StyleMask = [.closable, .titled, .miniaturizable]
    window?.styleMask = styleMask
}}

谢谢

EN

回答 1

Stack Overflow用户

发布于 2022-07-11 12:28:10

这样啊,原来是这么回事。在app委托中,使用deminiaturize函数做我想做的事情。

代码语言:javascript
复制
    for window in NSApp.windows {
        window.deminiaturize(nil)
    }

因为我只有一个窗口,但是NSApp.windows有两个成员,所以我认为我可以将所有的窗口去透明化。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72936710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档