You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UIMenuBuilder菜单栏与上下文菜单行为不符问题咨询

关于UIMenu构建的异常行为问题

官方文档说明:若要通过主菜单系统在菜单栏中添加或移除菜单,需在AppDelegate中重写buildMenu(with:)方法;若要通过上下文系统构建上下文菜单,需在视图控制器中重写该方法。

但实际开发中遇到了和文档描述不符的情况:所有菜单构建代码都写在AppDelegate中,大部分使用insertSibling()方法,仅一处是从零构建菜单:

let zoomInCommand = UIKeyCommand(title: NSLocalizedString("Zoom In", comment: ""),
                                 image: nil,
                                 action: #selector(ViewController.zoomIn),
                                 input: "+",
                                 modifierFlags: .command)
let zoomOutCommand = UIKeyCommand(title: NSLocalizedString("Zoom Out", comment: ""),
                                  image: nil,
                                  action: #selector(ViewController.zoomOut),
                                  input: "-",
                                  modifierFlags: .command)
let zoomCancelCommand = UIKeyCommand(title: NSLocalizedString("Reset Zoom", comment: ""),
                                  image: nil,
                                  action: #selector(ViewController.zoomCancel),
                                  input: "0",
                                  modifierFlags: .command)
let zoomMenu = UIMenu(title:"Zoom", identifier: UIMenu.Identifier("Zoom"), options:.displayInline ,children: [zoomInCommand,zoomOutCommand,zoomCancelCommand])
builder.insertChild(zoomMenu, atEndOfMenu: .view)

注:该zoomMenu与其他菜单一样,均在AppDelegate中构建。以下是使用insertSibling()方法的示例:

let saveProjectCommand = UIKeyCommand(title: NSLocalizedString("Save Project", comment: ""),
                                 image: nil,
                                 action: #selector(ViewController.saveProjectAndShowActivityIndicator),
                                 input: "p",
                                 modifierFlags: .command)
let saveImageCommand = UIKeyCommand(title: NSLocalizedString("Save Image", comment: ""),
                                 image: nil,
                                 action: #selector(ViewController.saveImageAction),
                                 input: "i",
                                 modifierFlags: .command)
let saveSVGCommand = UIKeyCommand(title: NSLocalizedString("Save SVG File", comment: ""),
                                 image: nil,
                                 action: #selector(ViewController.saveSVG),
                                 input: "g",
                                 modifierFlags: .command)
let saveMenu = UIMenu(title:"Save",identifier: UIMenu.Identifier(rawValue: "Save"),children: [saveProjectCommand,saveSVGCommand,saveImageCommand])
builder.insertSibling(saveMenu, beforeMenu: .close)

视图控制器中仅在validate函数里处理了三个缩放功能的显示逻辑。但运行应用时长按COMMAND键,会弹出包含缩放功能的上下文菜单(截图显示该菜单包含缩放选项)。

按照文档描述,在AppDelegate中定义的缩放菜单不应出现在视图控制器的上下文菜单中,但实际却出现了;另外,为什么保存菜单没有出现在这个上下文菜单里?

内容的提问来源于stack exchange,提问作者Victor Engel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 16:34:57