swift - 在 SwiftUI 中删除/替换/添加菜单
问题描述
我有一个带有 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()
}
}
}
解决方案
可以这样做:
@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")
}
}
}
}
}
推荐阅读
- yii - 我在文件解析方面遇到了一些问题
- c# - 如果 linq 查询未找到匹配记录,则对象引用未设置错误?
- python - 如何聚合来自不同行的分数并返回 Python 中的最大总分?
- c# - 如何在 TreeNode C# 中创建嵌套循环
- docker - 在 ubuntu 的 docker 上运行 filebeat
- sublimetext3 - 展开后是否可以使用侧按钮标记文本以进一步折叠/展开?
- vue.js - 获取Vuetify多选组件中单击项目的值
- ode - Maxima ODE求解器无法求解逻辑方程?
- android - 未知的命令行选项'--variant'
- actions-on-google - 如何在 Google Home 设备上测试我的操作