在SwiftUI中删除/替换/添加菜单

3

我有一个简单的SwfitUI(XCode 12.4,MacOS 11.1)Mac应用程序,并且有一个AppDelegate。

如何删除默认的文件/编辑/视图/窗口/帮助菜单并替换为自定义菜单?

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ notification: Notification) {
        let _ = NSApplication.shared.windows.map { $0.tabbingMode = .disallowed }
        let mainWindow = NSApplication.shared.windows.first!
        
        mainWindow.backgroundColor = NSColor(red: 1, green: 1, blue: 1, alpha: 0.3)
        mainWindow.titlebarAppearsTransparent = true
        mainWindow.titleVisibility = .hidden        
        mainWindow.backgroundColor = NSColor(red: 1, green: 1, blue: 1, alpha: 1)
    }
    
    func applicationWillFinishLaunching(_ notification: Notification) {
        NSWindow.allowsAutomaticWindowTabbing = false
    }
}

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

1
在我看来,你不应该这样做,我们Mac用户期望每个程序都有这些菜单。 - Joakim Danielson
1个回答

3
可以像这样完成:
@main
struct VeyBoardApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .commands {
            CommandGroup(replacing: CommandGroupPlacement.newItem) {}
            CommandGroup(replacing: CommandGroupPlacement.sidebar) {
                Button("New menu item in View") {
                    print("clicked")
                }
            }
        }
    }
}


3
这个答案确实有点帮助,因为它展示了如何移除/禁用菜单项。但是它并没有展示如何完全移除一个菜单。例如,我可以移除默认的“帮助”菜单项,但是菜单“帮助”仍然存在一个搜索框。你怎么才能完全移除“帮助”菜单呢? - G. Marc
@G.Marc https://dev59.com/1b7pa4cB1Zd3GeqP0oYL#70553784 - Andrew_STOP_RU_WAR_IN_UA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接