小编Iva*_*Fan的帖子

使用 SwiftUI 创建 MacOS 无窗口菜单栏应用程序

我正在寻找一种使用 SwiftUI 创建 Macos 无窗口菜单栏应用程序的解决方案。

我已经实现了与菜单栏相关的功能,问题是删除主窗口并从扩展坞中删除应用程序。

我尝试设置Application is agent (UIElement)YESin Info.plist,但它仅在窗口仍然存在时从扩展坞中隐藏应用程序。

我尝试修改@main,但它也不起作用。

有什么办法可以实现这一点吗?太感谢了!

我的代码:

  • App.swift
import 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)

macos xcode swift swiftui macos-big-sur

4
推荐指数
1
解决办法
3309
查看次数

标签 统计

macos ×1

macos-big-sur ×1

swift ×1

swiftui ×1

xcode ×1