View in English

  • Apple Developer
    • Get Started

    Explore Get Started

    • Overview
    • Learn
    • Apple Developer Program

    Stay Updated

    • Latest News
    • Hello Developer
    • Platforms

    Explore Platforms

    • Apple Platforms
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store

    Featured

    • Design
    • Distribution
    • Games
    • Accessories
    • Web
    • Home
    • CarPlay
    • Technologies

    Explore Technologies

    • Overview
    • Xcode
    • Swift
    • SwiftUI

    Featured

    • Accessibility
    • App Intents
    • Apple Intelligence
    • Games
    • Machine Learning & AI
    • Security
    • Xcode Cloud
    • Community

    Explore Community

    • Overview
    • Meet with Apple events
    • Community-driven events
    • Developer Forums
    • Open Source

    Featured

    • WWDC
    • Swift Student Challenge
    • Developer Stories
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Centers
    • Documentation

    Explore Documentation

    • Documentation Library
    • Technology Overviews
    • Sample Code
    • Human Interface Guidelines
    • Videos

    Release Notes

    • Featured Updates
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • tvOS
    • Xcode
    • Downloads

    Explore Downloads

    • All Downloads
    • Operating Systems
    • Applications
    • Design Resources

    Featured

    • Xcode
    • TestFlight
    • Fonts
    • SF Symbols
    • Icon Composer
    • Support

    Explore Support

    • Overview
    • Help Guides
    • Developer Forums
    • Feedback Assistant
    • Contact Us

    Featured

    • Account Help
    • App Review Guidelines
    • App Store Connect Help
    • Upcoming Requirements
    • Agreements and Guidelines
    • System Status
  • Quick Links

    • Events
    • News
    • Forums
    • Sample Code
    • Videos
 

Vídeos

Abrir menu Fechar menu
  • Coleções
  • Todos os vídeos
  • Sobre

Mais vídeos

  • Sobre
  • Código
  • Use SwiftUI with AppKit

    Discover how the Shortcuts app uses both SwiftUI and AppKit to create a top-tier experience on macOS. Follow along with the Shortcuts team as we explore how you can host SwiftUI views in AppKit code, handle layout and sizing, participate in the responder chain, enable navigational focus, and more. We'll also show you how to host AppKit views, helping you migrate existing code into a SwiftUI layout within your app.

    Recursos

    • UIKit integration
      • Vídeo HD
      • Vídeo SD

    Vídeos relacionados

    WWDC22

    • What's new in AppKit

    WWDC21

    • Direct and reflect focus in SwiftUI
  • Buscar neste vídeo...
    • 1:29 - SidebarView and SidebarItem

      struct SidebarView: View {
          @State private var selectedItem: SidebarItem
          
          var body: some View {
              List(selection: $selectedItem) {
                  ...
                  Section("Shortcuts") { ... }
                  Section("Folders") { ... }
              }
          }
      }
      
      enum SidebarItem: Hashable {
          case gallery
          case allShortcuts
          ...
          case folder(Folder)
      }
    • 1:53 - Hosting SwiftUI sidebar

      let splitViewController = NSSplitViewController()
      
      let sidebar = NSHostingController(rootView: SidebarView(...))
      let splitViewItem = NSSplitViewItem(viewController: sidebar)
      splitViewController.addSplitViewItem(splitViewItem)
    • 3:06 - Sidebar selection model

      class SelectionModel: ObservableObject {
      
          @Published var selectedItem: SidebarItem = .allShortcuts
      
      }
      
      // AppKit Window Controller
      cancellable = selectionModel.$selectedItem.sink { newItem in
          // update the NSSplitViewController detail
      }
    • 4:37 - Collection view item hosting SwiftUI

      class ShortcutItemView: NSCollectionViewItem {
          private var hostingView: NSHostingView<ShortcutView>?
      
          func displayShortcut(_ shortcut: Shortcut) {
              let shortcutView = ShortcutView(shortcut: shortcut)
      
              if let hostingView = hostingView {
                  hostingView.rootView = shortcutView
              } else {
                  let newHostingView = NSHostingView(rootView: shortcutView)
                  view.addSubview(newHostingView)
                  setupConstraints(for: newHostingView)
                  self.hostingView = newHostingView
              }
          }
      }
    • 7:55 - Popover presentation

      viewController.present(NSHostingController(rootView: ...), 
          asPopoverRelativeTo: rect, of: view, 
          preferredEdge: .maxY, behavior: .transient)
    • 8:15 - Sheet presentation

      viewController.presentAsSheet(NSHostingController(rootView: ...))
    • 8:22 - Modal window presentation

      let hostingController = NSHostingController(rootView: ModalView())
      hostingController.title = "Window Title"
      viewController.presentAsModalWindow(hostingController)
    • 8:45 - Sizing options

      hostingController.sizingOptions = [.minSize, .intrinsicContentSize, .maxSize]
    • 10:47 - Copy, Cut, and Paste commands

      Image(...)
          .focusable()
          .copyable { ... }
          .cuttable { ... }
          .pasteDestination(payloadType: Image.self) { ... }
    • 11:02 - Respond to standard commands

      struct ShortcutsEditorView: View {
          var body: some View {
              ScrollView { ... }
                  .onMoveCommand { moveSelection(direction: $0) }
                  .onExitCommand { cancelOperations() }
                  .onCommand(#selector(NSResponder.selectAll(_:)) { selectAllActions() }
                  .onCommand(#selector(moveActionUp(_:)) { moveSelectedAction(.up) }
                  .onCommand(#selector(moveActionDown(_:)) { moveSelectedAction(.down) }
          }
      }
    • 15:18 - Script editor

      class ScriptEditorView: NSView {
          var sourceCode: String
          var isEditable: Bool
          weak var delegate: ScriptEditorViewDelegate?
      }
      
      protocol ScriptEditorViewDelegate: AnyObject {
          func sourceCodeDidChange(in view: ScriptEditorView) -> Void
      }
    • 15:40 - Script editor container

      struct ScriptEditorContainerView: View {
          @State var sourceCode: String = ""
      
          var body: some View {
              VStack {
                  CompileButton { compile(code: sourceCode) }
                  Divider()
                  ScriptEditorRepresentable(sourceCode: $sourceCode)
              }
          }
      }
    • 16:13 - Script editor representable

      struct ScriptEditorRepresentable: NSViewRepresentable {
          @Binding var sourceCode: String
      
          func makeNSView(context: Context) -> ScriptEditorView {
              let scriptEditor = ScriptEditorView(frame: .zero)
              scriptEditor.delegate = context.coordinator
              return scriptEditor
          }
      
          func updateNSView(_ nsView: ScriptEditorView, context: Context) {
              if sourceCode != scriptEditor.sourceCode {
                  scriptEditor.sourceCode = sourceCode
              }
              scriptEditor.isEditable = context.environment.isEnabled
              // Make sure coordinator has a reference to the current value 
              // of the binding:
              context.coordinator.representable = self
          }
      
          func makeCoordinator() -> Coordinator {
              Coordinator(representable: self)
          }
      }
      
      class Coordinator: NSObject, ScriptEditorViewDelegate {
          var representable: ScriptEditorRepresentable
      
          init(representable: ScriptEditorRepresentable) { ... }
      
          func sourceCodeDidChange(in view: ScriptEditorView) {
              representable.sourceCode = view.sourceCode
          }
      }

Developer Footer

  • Vídeos
  • WWDC22
  • Use SwiftUI with AppKit
  • Open Menu Close Menu
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store
    Open Menu Close Menu
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    Open Menu Close Menu
    • Accessibility
    • Accessories
    • Apple Intelligence
    • Audio & Video
    • Augmented Reality
    • Business
    • Design
    • Distribution
    • Education
    • Games
    • Health & Fitness
    • In-App Purchase
    • Localization
    • Maps & Location
    • Machine Learning & AI
    • Security
    • Safari & Web
    Open Menu Close Menu
    • Documentation
    • Downloads
    • Sample Code
    • Videos
    Open Menu Close Menu
    • Help Guides & Articles
    • Contact Us
    • Forums
    • Feedback & Bug Reporting
    • System Status
    Open Menu Close Menu
    • Apple Developer
    • App Store Connect
    • Certificates, IDs, & Profiles
    • Feedback Assistant
    Open Menu Close Menu
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • Security Bounty Program
    • Security Research Device Program
    Open Menu Close Menu
    • Meet with Apple
    • Apple Developer Centers
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Academies
    • WWDC
    Read the latest news.
    Get the Apple Developer app.
    Copyright © 2026 Apple Inc. All rights reserved.
    Terms of Use Privacy Policy Agreements and Guidelines