我正在寻找一种使用 SwiftUI 创建 Macos 无窗口菜单栏应用程序的解决方案。
我已经实现了与菜单栏相关的功能,问题是删除主窗口并从扩展坞中删除应用程序。
我尝试设置Application is agent (UIElement)为YESin Info.plist,但它仅在窗口仍然存在时从扩展坞中隐藏应用程序。
我尝试修改@main,但它也不起作用。
有什么办法可以实现这一点吗?太感谢了!
我的代码:
App.swiftimport SwiftUI
@main
struct DiskHealthApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem: NSStatusItem?
var popOver = NSPopover()
func applicationDidFinishLaunching(_ notification: Notification) {
let menuView = ContentView()
popOver.behavior = .transient
popOver.animates = true
popOver.contentViewController = NSViewController()
popOver.contentViewController?.view = NSHostingView(rootView: menuView)
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if …Run Code Online (Sandbox Code Playgroud)