How do I create a menu bar application with SwiftUI?
Asked on 2024-07-30
1 search
To create a menu bar application with SwiftUI, you can leverage the new SwiftUI integrations with AppKit. Here are the steps to get you started:
-
Define Your Menu Using SwiftUI Views:
- Create your menu definition using a SwiftUI view in the body. Use SwiftUI views that best describe the data relationship, such as toggles, pickers, and buttons.
-
Initialize an
NSHostingMenu:- Use the
NSHostingMenuclass, which is a newNSMenusubclass, to initialize your menu with the SwiftUI view you defined.
- Use the
-
Use the Menu in AppKit Contexts:
- You can use this
NSHostingMenuin any AppKit context that accepts anNSMenu, such as the newNSPopupButtoninitializer with a pull-down menu parameter.
- You can use this
For more detailed information, you can refer to the session What’s new in AppKit at the timestamp 06:21.
Additionally, if you want to learn more about customizing macOS windows with SwiftUI, you can check out the session Tailor macOS windows with SwiftUI at the timestamp 00:53.
Relevant Sessions:
These sessions will provide you with a comprehensive understanding of how to integrate and customize SwiftUI menus and windows in your macOS applications.

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.

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.

SwiftUI essentials
Join us on a tour of SwiftUI, Apple’s declarative user interface framework. Learn essential concepts for building apps in SwiftUI, like views, state variables, and layout. Discover the breadth of APIs for building fully featured experiences and crafting unique custom components. Whether you’re brand new to SwiftUI or an experienced developer, you’ll learn how to take advantage of what SwiftUI has to offer when building great apps.
