我的问题很简单,我正在尝试删除此按钮
位于菜单栏的"file"部分。
这是我尝试过的:
WindowGroup("test") {
ContentView()
}.commands {
CommandGroup(replacing: .saveItem) {}
}
出于某种原因,这也适用于新项目。以下是我正在为应用程序的其他部分编辑的一些其他命令组
SidebarCommands()
CommandMenu("Launch") {
...
}
CommandGroup(replacing: .newItem) { }
CommandGroup(replacing: .toolbar) { }
CommandGroup(replacing: .appInfo) {
Button("About app") {
openWindow(id: "about")
}
}
CommandGroup(replacing: .appTermination) {
...
}
CommandGroup(after: .appSettings) {
Section {
CheckForUpdatesView(updater: updaterController.updater)
.environmentObject(networkMonitor)
}
}
CommandGroup(after: .newItem) {
Button("Add Item") {
showAddItem = true
}
}
如果此代码有帮助,我会将应用程序的“关于”部分更改为自定义部分:
Window("About", id: "about") {
AboutView()
.background {
if aboutWindow == nil {
Color.clear.onReceive(NotificationCenter.default.publisher(for:
NSWindow.didBecomeKeyNotification)) { notification in
if let aboutWindow = notification.object as? NSWindow {
if aboutWindow.title == "About" {
aboutWindow.standardWindowButton(.zoomButton)?.isHidden = true
}
}
}
}
}.frame(width: 400, height: 250)
}.windowResizability(.contentSize).windowStyle(.hiddenTitleBar)
我已经进行了研究,但找不到解决此问题的任何方法。
注意:我看到每一篇文章都说要完全按照我的尝试去做,但没有一个起作用,所以我只是在这里创建一个。
最佳答案
这里的问题是您有多个窗口。在每个窗口上,您必须说要删除 CommandGroup saveItem,这是一个示例。
WindowGroup() {
ContentView()
}.commands {
CommandGroup(replacing: .saveItem) {}
}
WindowGroup("Inspector") {
InspectorView()
}
此代码不起作用,因为检查器窗口组没有删除命令组,因此要修复它,您可以这样做:
WindowGroup("Inspector") {
InspectorView()
}.commands {
CommandGroup(replacing: .saveItem) {}
}
所以在你的情况下添加
.commands {
CommandGroup(replacing: .saveItem) {}
}
到您的“关于”窗口。
关于macos - 删除关闭 (CMD+W) 菜单栏按钮不起作用 SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75990536/