ios – How to add subview in NSMenuItem WITHOUT storyboard?

0
25

[ad_1]

I know there are answers like these, but almost everyone has their menu bar in storyboard. I made it purely in code without using .xib files so I’m not sure how to add a subview to my NSMeny item. My menu items show up like this:

  func setupMenus() {
    // 1
    let menu = NSMenu()

    let one = NSMenuItem(title: "Toggle", action: #selector(didTapOne) , keyEquivalent: "1")
    menu.addItem(one)
    
    let warning = NSMenuItem(title: "INSTRUCTIONS", action: #selector(didTapWarning) , keyEquivalent: "")
    
    menu.addItem(warning)
    
    menu.addItem(NSMenuItem.separator())
    
    menu.addItem(NSMenuItem(title: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))

}

which is defined in applicationDidFinishLaunching as follows:

    statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
    NSApp.hide(nil)
    if let button = statusItem.button {
        button.image = NSImage(systemSymbolName: "lock.shield", accessibilityDescription: "1")
    }

I’m not sure how can I add a subview to this? I just wish to add a line of text (that’s greyed out: because they’re instructions) and a custom button, both of which seems like can be achieved by subview but I’m not sure how to implement that. Keep in mind I ‘m not utilizing a storyboard since I’m building a menu bar app.

[ad_2]