跳转至
Structure

WindowGroup

WindowGroup is the default Scene type in SwiftUI.

Declaration

struct WindowGroup<Content> : Scene where Content : View

Overview

Use a WindowGroup to contain the SwiftUI view hierarchy of your app.

Usage

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            Text("Bananas")
                .frame(width: 500, height: 300)
        }
    }
}
A Mac view displaying a window group with the text "Bananas"; the text view has a fram of width 500 and height 300.

An iOS view displaying a window group with the text "Bananas"; the text view has a fram of width 500 and height 300.

Note:

  • The default implementation of a WindowGroup allows multiple instances of the window to be created (either using ⌘N , or the "Show Tab Bar" command).
  • Each instance of a window created from a window group contains the same SwiftUI hierarchy, but maintains an independent state. That means if the user creates two instances of your app's window, each window will maintain its own separate (independent) state, unaffected by what the user does on another window of your app.

Adding a title to a window

On macOS, a window's title is usually displayed in a window's title bar. A window's title bar contains a centered text item to display the window's title.

Use init(_:content:) to title a window. For example:

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup("Fruit App") {
            Text("Bananas")
                .frame(width: 500, height: 300)
        }
    }
}
A Mac view displaying a window group with the text "Bananas" and the window title "Fruit App"; the text view has a frame of width 500 and height 300.

  • Note: The system may use the provided window title as a part of some default window command names. For example, the "New Window" command would become "New Fruit App Window".

Adding commands to a window group

On macOS, a window can provide a set of contextual commands as menu items in the menu bar. To add a command menu to a WindowGroup, use commands(content:).

For example:

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            Text("Bananas")
                .frame(width: 500, height: 300)
        }
        .commands {
            CommandMenu("Some Commands") {
                Text("A Command")
            }
        }
    }
}
A Mac view displaying a window group with the text "Bananas" and the command menu option "Some Commands", which contains the text "A Command" in it's dropdown; the text view has a frame of width 500 and height 300.

Disabling creating multiple window instances

Currently, SwiftUI offers no canonical way to disable the user from creating multiple instances of a window from a WindowGroup scene. This can be done in two ways:

  • Using the "New Window" (⌘N) command to create a new window instance
  • Using the "Show Tab Bar" command, and creating a new tab containing a new window instance

Disabling the "New Window" command

The "New Window" (⌘N) command can be disabled by replacing the "New Item" command group with an empty command group.

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            Text("Bananas")
                .frame(width: 500, height: 300)
        }
        .commands {
            CommandGroup(replacing: .newItem, addition: { })
        }
    }
}
Caveat: This does not currently work on macCatalyst (as of Big Sur 11.2).

Disabling the "Show Tab Bar" command

To disable the "Show Tab Bar" command, use NSApplication to iterate over your app's windows at launch and set each window's tabbingMode to .disallowed.

For example:

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            Text("Bananas")
                .onAppear {
                    NSApplication.shared.windows.forEach({ $0.tabbingMode = .disallowed })
                }
        }
    }
}

Availability

iOS 14.0+

macOS 11.0+

tvOS 14.0+

watchOS 7.0+

Topics


Instance Property

body The content and behavior of the scene.


Type Alias

Body The type of scene that represents the body of this scene.


Initializer

init(_:content:) Creates a window group with a key for localized title string.

init(_:content:) Creates a window group with a title string.

init(_:content:) Creates a window group with a localized title.

init(_:id:content:) Creates a window group with a localized title and an identifier.

init(_:id:content:) Creates a window group with a key for localized title string and an identifier.

init(_:id:content:) Creates a window group with a title string and an identifier.

init(content:) Creates a window group.

init(id:content:) Creates a window group with an identifier.