how to create a menubar

Asked on 12/25/2024

1 search

To create a menubar in a macOS application using SwiftUI, you can utilize the integration of SwiftUI with AppKit. SwiftUI allows you to define menus that can be shared between different parts of your app. Here's a brief overview of how you can achieve this:

  1. SwiftUI Menus in AppKit: You can use SwiftUI to create menu definitions and integrate them into your AppKit-based macOS applications. This is done using the NSHostingMenu, a new subclass of NSMenu, which allows you to use SwiftUI views to define your menu items.

  2. Creating a Menu: Define your menu using SwiftUI views. You can use various SwiftUI components like Toggle, Picker, and Button to create interactive menu items. These components help describe the data relationships and actions within your menu.

  3. Integrating with AppKit: Once your menu is defined in SwiftUI, you can initialize an NSHostingMenu with your SwiftUI view. This allows you to seamlessly integrate the menu into your AppKit application, providing a consistent user experience across different parts of your app.

For more detailed guidance, you can refer to the session What’s new in AppKit which covers building menus with SwiftUI.

how to create a menubar | Ask WWDC