These instructions are for configuring a new project that was created using Apple's App template that comes with Xcode.
Since Xcode's starter project template for a macOS SwiftUI app isn't set up for MacMenuBar there are few things you have to change to make it ready.
-
Add
MacMenuBaras a Swift Package Dependency in your app. In Xcode selectSwift Packagesfrom theFilemenu, thenAdd Package Dependency. Then fill-in the URL for this package: https://github.com/chipjarred/MacMenuBar.git -
Remove the
Main.storyboardfile. Just like you don't use a Storyboard for your SwiftUIViewtypes, you don't use them forMacMenuBareither. Just delete it (or uncheck it as belonging to the application target in theFile Inspectorside-bar on the right). -
Change the
Main Interfacetarget setting.- Click on the project in the Project Navigator (side bar to the left that shows files and folder)
- Select your application target
- Select the "General" tab at the top, then in the "Deployment Info" section, clear the "Main Interface" field.
-
Add
main.swift. WithMain.storyboardgone,NSApplicationwon't work automagically, so you have to add amain.swiftto provide a working entry point for your app. It should look like this.
import Cocoa
let delegate = AppDelegate()
NSApplication.shared.delegate = delegate
_ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)- Remove the
@NSApplicationMainattribute fromAppDelegateinAppDelegate.swift.
@NSApplicationMain // <- REMOVE THIS
class AppDelegate: NSObject, NSApplicationDelegate- Import
MacMenuBarinAppDelelgate.swift:
import Cocoa
import SwiftUI
import MacMenuBar // <-- ADD THIS- Test the setup by building and running the app. You no longer have a menu bar in the app, so you'll need to kill it using Stop button in Xcode.
You're now ready to start building your menus.