【问题标题】:Remove / replace / add menus in SwiftUI在 SwiftUI 中删除/替换/添加菜单
【发布时间】:2023-03-23 20:53:01
【问题描述】:

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

如何删除默认的文件/编辑/查看/窗口/帮助菜单并用自定义菜单替换它们?

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()
        }
    }
}

【问题讨论】:

  • 在我看来你不应该这样做,我们 Mac 用户希望每个程序都有这些菜单

标签: swift macos swiftui


【解决方案1】:

可以这样做:

@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")
                }
            }
        }
    }
}

【讨论】:

  • 这个答案确实有点帮助,因为它显示了如何删除/禁用菜单项。但它没有显示如何完全删除菜单。例如,我可以删除默认的“帮助”菜单项,但菜单“帮助”仍然带有搜索字段。如何完全删除“帮助”菜单?
猜你喜欢
  • 2011-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多