1

I am trying to create an application that starts with a Status Bar icon, and without a application window. It is for a macOS utility that will be invoked from menus displayed from the Status Bar icon.

Can this be done entirely within SwiftUI, or must I use AppKit?

This is what it looks like in AppKit / Cocoa

theItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
let theStatusButton = theItem!.button
theStatusButton?.title = "MyApp"
    
let menuItemOne = NSMenuItem(title: "Hey MyApp", action: nil, keyEquivalent: "")
statusMenu = NSMenu(title: "This to do in MyApp")
statusMenu!.addItem(menuItemOne)
theItem!.menu = statusMenu!

Thanks in advance for any help.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • Everything concerning statusbar item and its menu should be done via NSStatusBar, but if you open some custom view, then that view can be implemented with SwiftUI. – Asperi Nov 30 '20 at 04:44
  • @Asperi thanks for confirming my suspicion on that. Given this and some of the other requirements that I have, SwiftUI may not be the ideal framework for my application. Thanks again. – LoneSTrider Nov 30 '20 at 12:14

1 Answers1

0

macOS 13.0+, Xcode 14.0+

Use SwiftUI's MenuBarExtra structure to create a Status Bar icon and menu on macOS Ventura.

struct MenuBarExtra<Label, Content> where Label : View, Content : View

Look at my post for details.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220