Panel

  • Select your xib interface, Drag a button into the view, and change button title to "Show Panel".
  • Declare panel variable in the controller class.
  • Define a function called showPanelAction, Connect button action to it, in where you can write show panel code.

The reference code is implemented as follows:

import Cocoa
class ViewController: NSViewController {
    
    lazy var panel: HDPanel = {
        let p = HDPanel(borderlesstRect: NSZeroRect, styleMask: [.borderless])
        p.isMovableByWindowBackground = true
        return p
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }
    
    
    @IBAction func showPanelAction(_ sender: Any) {
        if let button = sender as? NSButton {
            if let view = button.superview {
                var pointInWindow = view.convert(button.frame, to: nil)
                pointInWindow.size = NSSize(width: 200, height: 240)
                view.window?.addChildWindow(panel, ordered: .above)
                panel.setFrame(pointInWindow, display: true)
                panel.orderFront(self)
            }
        }
    }
}