How can I update the window menu in SwiftUI?
Asked on 2024-08-03
1 search
To update the window menu in SwiftUI, you can use the new NSHostingMenu
class, which is an NSMenu
subclass. This allows you to create your menu definition using a SwiftUI view and then use it in any AppKit context that accepts an NSMenu
.
Here's a step-by-step guide:
-
Create your menu definition using a SwiftUI view:
- Use SwiftUI views that best describe the data relationship.
- Use a
Toggle
to switch a value on and off, aPicker
to select one value from a list, and aButton
to perform an action.
-
Initialize an
NSHostingMenu
with that SwiftUI view:- This allows you to share menu definitions between the parts of your app that use AppKit and the parts that use SwiftUI.
For more details, you can refer to the session What’s new in AppKit at the timestamp 06:21.
Relevant Sessions

What’s new in SwiftUI
Learn how you can use SwiftUI to build great apps for any Apple platform. Explore a fresh new look and feel for tabs and documents on iPadOS. Improve your window management with new windowing APIs, and gain more control over immersive spaces and volumes in your visionOS apps. We’ll also take you through other exciting refinements that help you make expressive charts, customize and layout text, and so much 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.

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.