【问题标题】:How to make key shortcut groups in ios/catalyst如何在 ios/catalyst 中创建快捷键组
【发布时间】:2021-12-23 00:39:13
【问题描述】:

在我的应用程序中,我使用 UIViewController.addKeyCommand() 创建键盘快捷键。当用户按住 CMD 时,会自动显示一个包含所有可用命令的漂亮表格。但是在 iOS 15 中,这些表可以按组排序。至少系统表是:

(组系统、多任务、拆分视图、滑过)。是否可以在我的应用程序中进行此分组?如果是,怎么做?

【问题讨论】:

    标签: keyboard-shortcuts ios15


    【解决方案1】:

    让它工作,但它需要与 UIViewController.addKeyCommand() 不同的机制

    1. 在 AppDelegate 中,覆盖 func buildMenu()

    2. 在内部,创建一个或多个 UIMenu 对象,并使用 UIKeyCommandObjects 填充它们的 children 属性。

    3. 使用 builder.insertSibling() 或类似方法在构建器中插入菜单。

    4. 就是这样。这样添加到菜单的每个 UIKeyCommand 都会像 UIViewController.addKeyCommand() 一样工作。每个 UIMenu 都将作为一组命令工作,其中 UIMenu.title 属性是该组的标题。

       override func buildMenu(with builder: UIMenuBuilder) {
           super.buildMenu(with: builder)
      
           if (builder.system != .main) {
               return
           }
      
           let cmd = UIKeyCommand(title: "TestCmd", action: #selector(MainViewController.ref!.actTest(sender:)), input: "x", discoverabilityTitle: "Test command")
      
           let menu = UIMenu(title: NSLocalizedString("Test group", comment: "hotkey group name"),
                         image: nil, identifier: UIMenu.Identifier(rawValue: "myapp.menu.ios.test"),
                         children: [cmd])
          builder.insertSibling(menu, afterMenu: .view)
      
       }
      

    【讨论】:

      猜你喜欢
      • 2013-07-22
      • 2021-02-14
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      相关资源
      最近更新 更多