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
MacMenuBar
as a Swift Package Dependency in your app. In Xcode selectSwift Packages
from theFile
menu, thenAdd Package Dependency
. Then fill-in the URL for this package: https://github.com/chipjarred/MacMenuBar.git -
Remove the
Main.storyboard
file. Just like you don't use a Storyboard for your SwiftUIView
types, you don't use them forMacMenuBar
either. Just delete it (or uncheck it as belonging to the application target in theFile Inspector
side-bar on the right). -
Change the
Main Interface
target 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.storyboard
gone,NSApplication
won't work automagically, so you have to add amain.swift
to 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
@NSApplicationMain
attribute fromAppDelegate
inAppDelegate.swift
.
@NSApplicationMain // <- REMOVE THIS
class AppDelegate: NSObject, NSApplicationDelegate
- Import
MacMenuBar
inAppDelelgate.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.