Last active
September 2, 2022 15:09
-
-
Save steipete/b8820036dff1761ad6392e57f5901473 to your computer and use it in GitHub Desktop.
Create menu in mac Catalyst via UIMenuBuilder (beta 4) - (void)buildMenuWithBuilder:(id<UIMenuBuilder>)builder;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AppDelegate.swift: | |
// macCatalyst: Create menu | |
override func buildMenu(with builder: UIMenuBuilder) { | |
guard builder.system == .main else { return } | |
// The format menu doesn't make sense | |
builder.remove(menu: .format) | |
// Add Open command | |
let openKeyCommand = PSVMenuBuilder.keyCommand(withTitle: "Open...", image:nil, action: #selector(openFile), input: "o", modifierFlags: [UIKeyModifierFlags.command], propertyList: nil) | |
// This results in an infinite loop inside init | |
// FB6790921: Catalyst: Creating UIKeyCommand in Swift results in an endless loop | |
//let openKeyCommand = UIKeyCommand(title: "Open...", image:nil, action: #selector(openFile), input: "o", modifierFlags: [UIKeyModifierFlags.command], propertyList: nil) | |
let identifier = UIMenu.Identifier("open_file") | |
let menu = UIMenu.init(title: "Open", image: nil, identifier: identifier, options: [.displayInline], children: [openKeyCommand]); | |
builder.insertSibling(menu, afterMenu: .newScene) | |
} |
Figured it out in case anyone is curious: you have to make the UIKeyCommands you want to put in a section children of a UIMenu, and use the UIMenuOptionsDisplayInline option for the menu.
@bmueller first I was like "I already did that and it didn't work" but then I was like, "this must be right, try harder" and the answer was... you're right. Some confusing things are that the IDs have to be unique otherwise the menus collapse, you must use insertSibling at some point, and... not sure.
THANKS for the comment here, it worked great for me to get separators in menus.
THANKS @steipete for the code, it's nice. I'm doing it all in Swift now (2022) so I think that's all sorted.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This has been fixed in b5. Haven't needed separators yet.