使用 SwiftUI 有选择地删除 macOS 应用程序中的默认菜单栏菜单(如“文件”、“编辑”和“查看”)

问题描述 投票:0回答:1

是否有一种

SwiftUI
惯用的方法可以有选择地从 macOS 应用程序的菜单栏中删除默认(
File
Edit
View
)菜单,同时保持其他菜单(如 AppName 菜单)不变?我正在构建的应用程序是一个简单的实用程序,因此
Edit
View
菜单与此上下文无关。

我知道新的

.commandsRemoved()
自 macOS 13 起可用,但这显然删除了所有菜单。

我正在寻找一种纯粹的

SwiftUI
解决方案。任何指导和帮助将不胜感激。

macos swiftui menu menubar
1个回答
0
投票

您可以尝试使用

.commandsRemoved()
删除所有命令, 然后只添加回您想要的那些。 您还可以使用条件显示/隐藏特定的
command

 @main
 struct TestMacApp: App {
     @State var showView = false
     
     var body: some Scene {
         WindowGroup {
             ContentView()
         }
         .commandsRemoved()
         .commandsReplaced {
             CommandGroup(replacing: .help, addition: {})
             if showView { CommandGroup(replacing: .textEditing, addition: {}) }
             // ...
         }
     }
 }
© www.soinside.com 2019 - 2024. All rights reserved.