how do I create a menu bar app

Generated on 8/1/2024

1 search

To create a menu bar app on macOS, you can use SwiftUI and AppKit together. Here are the steps to get you started:

  1. Create a SwiftUI App: Start by creating a new SwiftUI project in Xcode.

  2. Define the Menu: Use SwiftUI to define your menu. You can use NSHostingMenu to integrate SwiftUI menus into your AppKit context. This allows you to share menu definitions between different parts of your app.

    import SwiftUI
    
    struct MyMenu: View {
        var body: some View {
            Menu("My Menu") {
                Button("Action 1", action: performAction1)
                Button("Action 2", action: performAction2)
            }
        }
    
        func performAction1() {
            // Action 1 code
        }
    
        func performAction2() {
            // Action 2 code
        }
    }
    
  3. Integrate with AppKit: Use NSHostingMenu to integrate the SwiftUI menu into your AppKit application.

    import AppKit
    import SwiftUI
    
    class AppDelegate: NSObject, NSApplicationDelegate {
        var statusBarItem: NSStatusItem?
    
        func applicationDidFinishLaunching(_ notification: Notification) {
            let menu = NSMenu()
            let hostingMenu = NSHostingMenu(rootView: MyMenu())
            menu.addItem(withTitle: "My Menu", action: nil, keyEquivalent: "").submenu = hostingMenu
    
            statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
            statusBarItem?.button?.title = "Menu Bar App"
            statusBarItem?.menu = menu
        }
    }
    
  4. Set Up the App Delegate: Ensure your AppDelegate is set up correctly in your @main struct.

    @main
    struct MyApp: App {
        @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    

For more detailed information on integrating SwiftUI with AppKit, you can refer to the session What’s new in AppKit.

Relevant Sessions

  1. What’s new in AppKit
  2. Tailor macOS windows with SwiftUI
  3. Work with windows in SwiftUI

These sessions provide insights into using SwiftUI with AppKit, customizing window behaviors, and refining window placements, which are essential for creating a polished menu bar app.

Design great visionOS apps

Design great visionOS apps

Find out how to create compelling spatial computing apps by embracing immersion, designing for eyes and hands, and taking advantage of depth, scale, and space. We’ll share several examples of great visionOS apps and explore how their designers approached creating new experiences for the platform.

Dive deep into volumes and immersive spaces

Dive deep into volumes and immersive spaces

Discover powerful new ways to customize volumes and immersive spaces in visionOS. Learn to fine-tune how volumes resize and respond to people moving around them. Make volumes and immersive spaces interact through the power of coordinate conversions. Find out how to make your app react when people adjust immersion with the Digital Crown, and use a surrounding effect to dynamically customize the passthrough tint in your immersive space experience.

What’s new in AppKit

What’s new in AppKit

Discover the latest advances in Mac app development. Get an overview of the new features in macOS Sequoia, and how to adopt them in your app. Explore new ways to integrate your existing code with SwiftUI. Learn about the improvements made to numerous AppKit controls, like toolbars, menus, text input, and more.

Work with windows in SwiftUI

Work with windows in SwiftUI

Learn how to create great single and multi-window apps in visionOS, macOS, and iPadOS. Discover tools that let you programmatically open and close windows, adjust position and size, and even replace one window with another. We’ll also explore design principles for windows that help people use your app within their workflows.

Platforms State of the Union

Platforms State of the Union

Discover the newest advancements on Apple platforms.

Elevate your tab and sidebar experience in iPadOS

Elevate your tab and sidebar experience in iPadOS

iPadOS 18 introduces a new navigation system that gives people the flexibility to choose between using a tab bar or sidebar. The newly redesigned tab bar provides more space for content and other functionality. Learn how to use SwiftUI and UIKit to enable customization features – like adding, removing and reordering tabs – to enable a more personal touch in your app.

What’s new in UIKit

What’s new in UIKit

Explore everything new in UIKit, including tab and document launch experiences, transitions, and text and input changes. We’ll also discuss better-than-ever interoperability between UIKit and SwiftUI animations and gestures, as well as general improvements throughout UIKit.

Tailor macOS windows with SwiftUI

Tailor macOS windows with SwiftUI

Make your windows feel tailor-made for macOS. Fine-tune your app’s windows for focused purposes, ease of use, and to express functionality. Use SwiftUI to style window toolbars and backgrounds. Arrange your windows with precision, and make smart decisions about restoration and minimization.