Skip to main content

How to open a new window

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to open a new window 관련

SwiftUI by Example

Back to Home

How to open a new window | SwiftUI by Example

How to open a new window

Updated for Xcode 15

SwiftUI provides an openWindow environment key that allows us to create new windows on macOS whenever we need them.

To get started, first edit your App scene to include a new Window. This means providing a window title, but also an identifier – a name we’ll use when asking the system to open this window:

Window("What's New", id: "whats-new") {
    Text("New in this version…")
}

That can be the only scene in your app body, or it can live alongside other windows or window groups. If it is the only window in your app, macOS will automatically remove the File > New menu item, and your app will automatically terminate when the last window is closed.

Tips

If you add an explicit navigationTitle() to your window contents, that will override any window title string.

When you want to open that window, call the openWindow environment key with the same ID value, like this:

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

    var body: some View {
        Button("Show What's New") {
            openWindow(id: "whats-new")
        }
    }
}

The window can also be opened by going to the Window menu – macOS will automatically show it there, using the window title you provided.

Similar solutions…
How to open web links in Safari | SwiftUI by Example

How to open web links in Safari
How to present a new view using sheets | SwiftUI by Example

How to present a new view using sheets
How to push a new view when a list row is tapped | SwiftUI by Example

How to push a new view when a list row is tapped
How to combine shapes to create new shapes | SwiftUI by Example

How to combine shapes to create new shapes
How to push a new view onto a NavigationStack | SwiftUI by Example

How to push a new view onto a NavigationStack

이찬희 (MarkiiimarK)
Never Stop Learning.