Popup Menu

  • Select your xib interface, Drag a button into the view, and change button title to "Show Menu".
  • Declaring hMenu variable in the controller class, implementing a function named configMemu in which menu items are defined.
  • Define a function called showMenuAction, Connect button action to it, in where you can write open menu code.

The reference code is implemented as follows:

import Cocoa

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }
    
    override func viewDidDisappear() {
        super.viewDidDisappear()
        hMenu?.hide()
        hMenu = nil
    }
    
    
    var hMenu: HDPopMenu? = {
        let menu =   HDPopMenu(title: "")
        menu.debugMode = false
        menu.isDarkMode = false
        return menu
    }()
    
    func configMemu() {
        let newMenuItem = HDPopMenuItem(title: "New")
        let openMenuItem = HDPopMenuItem(title: "Open")
        openMenuItem.target = self
        openMenuItem.action = #selector(self.action(_:))
        let saveMenuItem = HDPopMenuItem(title: "Save")
        let saveAsMenuItem = HDPopMenuItem(title: "SaveAs...")
        let separator    = HDPopMenuItem.separator()
        let exitMenuItem = HDPopMenuItem(title: "Exit")
        let exportMenuItem = HDPopMenuItem(title: "Export")
        let duplicateMenuItem = HDPopMenuItem(title: "Duplicate")
        let showFinderMenuItem = HDPopMenuItem(title: "Show In Finer")
        
        let subMenu: HDPopMenu = HDPopMenu(title: "")
        saveMenuItem.submenu = subMenu
        
        let saveXMLMenuItem = HDPopMenuItem(title: "XML")
        let savePlistMenuItem = HDPopMenuItem(title: "Plist")
        let saveJsonMenuItem = HDPopMenuItem(title: "JSON")
        
        subMenu.addItem(saveXMLMenuItem)
        subMenu.addItem(savePlistMenuItem)
        subMenu.addItem(separator)
        subMenu.addItem(saveJsonMenuItem)
        
        let exportSubMenu: HDPopMenu = HDPopMenu(title: "")
        exportMenuItem.submenu = exportSubMenu
        
        let pdfMenuItem = HDPopMenuItem(title: "PDF")
        let epubMenuItem = HDPopMenuItem(title: "Epub")
        let mobiMenuItem = HDPopMenuItem(title: "Mobi")
        exportSubMenu.addItem(pdfMenuItem)
        exportSubMenu.addItem(epubMenuItem)
        exportSubMenu.addItem(mobiMenuItem)
        
        let saveJsonSubMenu: HDPopMenu = HDPopMenu(title: "")
        saveJsonMenuItem.submenu = saveJsonSubMenu
        let stadardMenuItem = HDPopMenuItem(title: "Standard")
        let advanstMenuItem = HDPopMenuItem(title: "Advanced")
        saveJsonSubMenu.addItem(stadardMenuItem)
        saveJsonSubMenu.addItem(advanstMenuItem)
        
        hMenu?.addItem(newMenuItem)
        hMenu?.addItem(openMenuItem)
        hMenu?.addItem(separator)
        hMenu?.addItem(saveMenuItem)
        hMenu?.addItem(separator)
        hMenu?.addItem(saveAsMenuItem)
        hMenu?.addItem(separator)
        hMenu?.addItem(exportMenuItem)
        hMenu?.addItem(separator)
        hMenu?.addItem(duplicateMenuItem)
        hMenu?.addItem(showFinderMenuItem)
        hMenu?.addItem(separator)
        hMenu?.addItem(exitMenuItem)
        
        for item in (hMenu?.items)! {
            item.target = self
            item.action = #selector(self.action(_:))
        }
        
        
    }
    
    @IBAction func showMenuAction(_ sender: Any) {
        if let button = sender as? NSButton {
            if let hMenu = hMenu, hMenu.items.count <= 0 {
                configMemu()
            }
            if let view = button.superview {
                let pointInWindow = view.convert(button.frame, to: nil)
                let pointInScreen = view.window!.convertToScreen(pointInWindow).origin
                hMenu?.update(positioning: nil, at: pointInScreen, in: view)
            }
        }
    }
    
    @IBAction func action(_ sender: Any) {
        
    }
    
}

重要提示:

HDPopMenuPanel继承自 HDRoundedPanel类,他需要创建一个Panel控件来生成.