0

In my app, I am using UIViewController.addKeyCommand() to create keyboard shortcuts. When user holds CMD, a nice table of all commands available is automatically shown. However in iOS 15, these table can be ordered into groups. At least system tables are: enter image description here

(groups System, Multitasking, Split view, Slide over). Is it possible to do this grouping also in my app? If yes, how?

Kaven
  • 147
  • 1
  • 11

1 Answers1

2

Got it working, but it needs different mechanism then UIViewController.addKeyCommand()

  1. In AppDelegate, override func buildMenu()

  2. Inside, create one or more UIMenu objects, and populate their children property with UIKeyCommandObjects .

  3. Insert menus in builder using builder.insertSibling() or similar.

  4. That's all. Every UIKeyCommand thus added to menu will work as if it was added by UIViewController.addKeyCommand(). And every UIMenu will work as a group of commands, where UIMenu.title property is the title of the group.

     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)
    
     }
    
Kaven
  • 147
  • 1
  • 11