-
-
Save steipete/b8820036dff1761ad6392e57f5901473 to your computer and use it in GitHub Desktop.
// 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) | |
} |
steipete
commented
Jul 31, 2019
via email
Have you figured out how to add separators yet?
This has been fixed in b5. Haven't needed separators yet.
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.