Question: What is SwiftUI API for creating status bar menus?

Apple seems to use SwiftUI views in Battery & WiFi menus according to the accessibility inspector. Screenshot of a battery menu attached, also its view hierarchy.

[![Battery menu screenshot](https://i.stack.imgur.com/5vqXC.png)](https://i.stack.imgur.com/5vqXC.png)
[![View hierarchy of the battery menu](https://i.stack.imgur.com/bweVy.png)](https://i.stack.imgur.com/bweVy.png)

**EDIT:**

Posted the solution as a separate answer.

Answer: 
Inside the AppDelegate add the following code:

```
// Create the status item in the Menu bar 
self.statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(NSStatusItem.variableLength))

// Add a menu and a menu item
let menu = NSMenu()
let editMenuItem = NSMenuItem()
editMenuItem.title = "Edit"
menu.addItem(editMenuItem)

//Set the menu 
self.statusBarItem.menu = menu

//This is the button which appears in the Status bar
if let button = self.statusBarItem.button {
    button.title = "Here"
}

```

This will add a Button with a custom Menu to your MenuBar.

[![enter image description here](https://i.stack.imgur.com/IbXjt.png)](https://i.stack.imgur.com/IbXjt.png)

**Edit - How to use SwiftUI View**

As you asked, here is the answer how to use a SwiftUI View.

First create a NSPopover and then wrap your SwiftUI view inside a `NSHostingController`.

```
var popover: NSPopover

let popover = NSPopover()
popover.contentSize = NSSize(width: 350, height: 350)
popover.behavior = .transient
popover.contentViewController = NSHostingController(rootView: contentView)
    self.popover = popover

```

Then instead of showing a NSMenu, toggle the popover:

```
self.statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(NSStatusItem.variableLength))
if let button = self.statusBarItem.button {
     button.title = "Click"
     button.action = #selector(showPopover(_:))
}

```

With following action:

```
@objc func showPopover(_ sender: AnyObject?) {
    if let button = self.statusBarItem.button
    {
        if self.popover.isShown {
            self.popover.performClose(sender)
        } else {
            self.popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
        }
    }
}

```

[![enter image description here](https://i.stack.imgur.com/usNL8.png)](https://i.stack.imgur.com/usNL8.png)
MenuBarExtra
------------

In macOS 13.0+ and Xcode 14.0+, the [MenuBarExtra](https://developer.apple.com/documentation/SwiftUI/MenuBarExtra) struct allows you create a system menu bar, that is similar to NSStatusBar's icons and menus. MenuBarExtra's item will be displayed in the system menu bar when the specified binding is set to `true`.

[![enter image description here](https://i.stack.imgur.com/bngnt.jpg)](https://i.stack.imgur.com/bngnt.jpg)

```
import SwiftUI

@available(macOS 13.0, *)                       // macOS Ventura
@main struct StatusBarApp: App {

    @State private var command: String = "A"

    var body: some Scene {

        MenuBarExtra(command, systemImage: "\(command).circle") {

            Button("Uno") { command = "A" }
                .keyboardShortcut("U")

            Button("Dos") { command = "B" }
                .keyboardShortcut("D")

            Divider()

            Button("Salir") { NSApplication.shared.terminate(nil) }
                .keyboardShortcut("S")
        }
    }
}

```

Getting rid of the App's icon from the Dock
-------------------------------------------

In Xcode's `Info` tab, choose `Application is agent (UIElement)` and set its value to `YES`.

[![enter image description here](https://i.stack.imgur.com/hktWN.png)](https://i.stack.imgur.com/hktWN.png)