How can I open a new window in a SwiftUI app on macos?

Generated on 8/5/2024

1 search

To open a new window in a SwiftUI app on macOS, you can use the openWindow environment action. Here’s a step-by-step guide based on the information from the WWDC sessions:

  1. Define a Window Group: First, define a new window group in your app. This window group will contain the content you want to display in the new window.

    struct MovieWindow: Scene {
        var body: some Scene {
            WindowGroup("Movie") {
                MovieView()
            }
            .windowStyle(.plain)
        }
    }
    
  2. Retrieve the openWindow Action: In the view where you want to trigger the opening of the new window, retrieve the openWindow action from the environment.

    struct ContentView: View {
        @Environment(\.openWindow) private var openWindow
    
        var body: some View {
            Button("Open Movie Window") {
                openWindow(id: "movie")
            }
        }
    }
    
  3. Use the openWindow Action: Use the openWindow action within a button or any other interactive element to open the new window.

    Button("Open Movie Window") {
        openWindow(id: "movie")
    }
    

Here’s a more detailed example from the session Work with windows in SwiftUI:

struct ContentView: View {
    @Environment(\.openWindow) private var openWindow

    var body: some View {
        Button("Open Movie Window") {
            openWindow(id: "movie")
        }
    }
}

In this example, the openWindow action is used to open a new window with the ID "movie". This ID corresponds to the window group defined earlier.

For more details, you can watch the session Work with windows in SwiftUI.

What’s new in AppKit

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.

Tailor macOS windows with SwiftUI

Tailor macOS windows with SwiftUI

Make your windows feel tailor-made for macOS. Fine-tune your app’s windows for focused purposes, ease of use, and to express functionality. Use SwiftUI to style window toolbars and backgrounds. Arrange your windows with precision, and make smart decisions about restoration and minimization.

What’s new in SwiftUI

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.

Work with windows in SwiftUI

Work with windows in SwiftUI

Learn how to create great single and multi-window apps in visionOS, macOS, and iPadOS. Discover tools that let you programmatically open and close windows, adjust position and size, and even replace one window with another. We’ll also explore design principles for windows that help people use your app within their workflows.

SwiftUI essentials

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.