创建单个子菜单栏
func createSingleSubMenu() -> (subMenu: NSMenu, menuItem: NSMenuItem) {
let submenu = NSMenu()
let mainDropdown = NSMenuItem(title: "1-2", action: nil, keyEquivalent: "")
submenu.addItem(NSMenuItem(title: "1-2-1", action: nil, keyEquivalent: ""))
submenu.addItem(NSMenuItem(title: "1-2-2", action: nil, keyEquivalent: ""))
return (submenu, mainDropdown)
}
创建包含多个子菜单栏的父菜单栏
func createMultiplySubMenu() -> NSMenu {
let main = NSMenu()
for _ in 0...6 {
let singleSubMenu = createSingleSubMenu()
main.addItem(singleSubMenu.menuItem)
main.setSubmenu(singleSubMenu.subMenu, for: singleSubMenu.menuItem)
}
return main
}
创建最上层的父菜单栏
func menu() -> NSMenu {
let main = NSMenu()
let submenu = createMultiplySubMenu()
let mainDropdown = NSMenuItem(title: "1", action: nil, keyEquivalent: "")
main.addItem(mainDropdown)
main.setSubmenu(submenu, for: mainDropdown)
return main
}