appkit swiftui integration. give me details

Asked on 06/11/2025

1 search

Apple has been enhancing the integration between AppKit and SwiftUI, allowing developers to incrementally adopt SwiftUI in their Mac apps. Here are some key details about this integration:

  1. SwiftUI Menus in AppKit: In macOS Sequoia, you can use SwiftUI menus within AppKit contexts using NSHostingMenu, a new NSMenu subclass. This allows you to share menu definitions between parts of your app that use AppKit and those that use SwiftUI. You can create your menu definition using a SwiftUI view and initialize an NSHostingMenu with it. This is covered in the session What’s new in AppKit.

  2. Animations: SwiftUI animations can now be used to animate NSView in AppKit. This integration allows you to use the full set of powerful SwiftUI animation types, including custom animations, to animate your AppKit views. This is discussed in the session Enhance your UI animations and transitions.

  3. Gesture Recognition: Gesture recognition has been factored out of UIKit, enabling you to use any built-in or custom UI gesture recognizer in your SwiftUI view hierarchy. This interoperability is crucial for apps that use both SwiftUI and AppKit. More details can be found in the session Platforms State of the Union.

  4. View Representables: If there's a view or view controller from AppKit that you want to use in SwiftUI, you can create a view representable. This is a special SwiftUI view protocol for creating and updating an associated AppKit view using imperative code. Conversely, you can embed a SwiftUI view into an AppKit view hierarchy using classes such as HostingViewController. This is explained in the session SwiftUI essentials.

These enhancements make it easier to integrate SwiftUI into existing AppKit applications, allowing developers to take advantage of SwiftUI's declarative syntax and powerful features while maintaining compatibility with existing AppKit code.