Home > Blockchain >  How to use UIMenuBuilder to insert new menu in menu bar in Mac Catalyst app
How to use UIMenuBuilder to insert new menu in menu bar in Mac Catalyst app

Time:08-03

You can use UIMenuBuilder to create a menu bar on iPadOS that’s similar to the traditional menu bar from macOS. You can insert key commands into standard menus such as Edit or you can create your own menu and insert it as a sibling after another menu. This works perfectly well on iPadOS, but on Mac Catalyst I can't seem to create any new menus, but I can add menu items to the Edit menu. Is there something different that needs to be done for the Mac?

Here you can see I've added a Search option to the Edit menu which works on iPadOS and macOS, and I’ve added an Image menu containing a Favorite option after the Edit menu which works on iPadOS but is unexpectedly missing from the menu bar on macOS.

override func buildMenu(with builder: UIMenuBuilder) {
    super.buildMenu(with: builder)
    
    guard builder.system == .main else { return }
    
    let editMenu = UIMenu(options: .displayInline, children: [
        UIKeyCommand(title: "Search", action: #selector(search), input: "F", modifierFlags: .command)
    ])
    builder.insertChild(editMenu, atStartOfMenu: .edit)
    
    let imageMenu = UIMenu(title: "Image", options: .displayInline, children: [
        UIKeyCommand(title: "Favorite", action: #selector(toggleFavorite), input: ".")
    ])
    builder.insertSibling(imageMenu, afterMenu: .edit)
}

@objc func search() {
    print("search")
}

@objc func toggleFavorite() {
    print("favorite")
}

iPadOS screenshot macOS screenshot

CodePudding user response:

Found the issue - removing the .displayInline option gets the Image menu to appear in the menu bar as expected.

  • Related