Joe*_*yLD 6 swiftui macos-big-sur
我正在(尝试)将我的 AppDelegate macOS 应用程序切换到 SwiftUI 生命周期 - 但似乎无法找出如何处理 CommandMenu。我只想删除这些默认菜单项(Fie、Edit、View 等...)。过去,我只是从故事板中删除它们 - 但我在这里不使用故事板。有没有办法在 SwiftUI 中删除这些项目?
我要删除的项目:
我知道如何通过以下方式添加新项目:
.commands {
MyAppMenus()
}
Run Code Online (Sandbox Code Playgroud)
但这只是将它们添加到现有的菜单项中。
swiftUI -- 使用您的自定义覆盖 AppDelegate:
@main
struct PixieApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
///........
}
Run Code Online (Sandbox Code Playgroud)
appDelegate的代码:
final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillUpdate(_ notification: Notification) {
DispatchQueue.main.async {
if let menu = NSApplication.shared.mainMenu {
menu.items.removeFirst{ $0.title == "Edit" }
menu.items.removeFirst{ $0.title == "File" }
menu.items.removeFirst{ $0.title == "Window" }
menu.items.removeFirst{ $0.title == "View" }
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
您可以通过 AppDelegate 文件删除命令菜单项:
override func buildMenu(with builder: UIMenuBuilder) {
super.buildMenu(with: builder)
builder.remove(menu: .services)
builder.remove(menu: .format)
builder.remove(menu: .toolbar)
}
Run Code Online (Sandbox Code Playgroud)
Apple 开发者论坛上的此主题也可能有所帮助:https ://developer.apple.com/forums/thread/649096
| 归档时间: |
|
| 查看次数: |
1959 次 |
| 最近记录: |