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:

  1. 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, a Picker to select one value from a list, and a Button to perform an action.
  2. 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