9

我正在尝试按照本教程创建一个仅出现在 MacOS Big Sur 菜单栏(右上角)中的应用程序:https ://medium.com/@acwrightdesign/creating-a-macos-menu-bar-application -使用-swiftui-54572a5d5f87/。它在 Xcode 11 和 MacOS Catalina 上工作,因为有一个 AppDelegate.swift 文件,但我听说它被这个方法取代了:

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

但是,本教程的前几个步骤要求我对(现在不存在的)AppDelegate.swift 文件进行一些更改。我已经尝试在 MyApp.swift 中进行这些更改,但我似乎无法让它工作。有人愿意帮我改编 MacOS Big Sur/Xcode 12 的教程吗?

注意:这里是 AppDelegate.swift 文件的外观(如果它存在的话)根据教程(如果你不想打开教程):

import Cocoa
import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var popover: NSPopover!
    var statusBarItem: NSStatusItem!
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView()

        // Create the popover
        let popover = NSPopover()
        popover.contentSize = NSSize(width: 400, height: 500)
        popover.behavior = .transient
        popover.contentViewController = NSHostingController(rootView: contentView)
        self.popover = popover
        
        // Create the status item
        self.statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(NSStatusItem.variableLength))
        
        if let button = self.statusBarItem.button {
            button.image = NSImage(named: "Icon")
            button.action = #selector(togglePopover(_:))
        }
    }
    
    @objc func togglePopover(_ sender: AnyObject?) {
        if let button = self.statusBarItem.button {
            if self.popover.isShown {
                self.popover.performClose(sender)
            } else {
                self.popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
            }
        }
    }
    
}
4

1 回答 1

12

在您的 App 场景中,使用 NSApplicationDelegateAdaptor 属性包装器告诉 SwiftUI 它应该使用您的 AppDelegate 类作为应用程序委托。所以你的 App 类应该是这样的:

@main
struct MyApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

知识库链接

于 2021-02-19T17:29:32.957 回答