This article explains how to set the window default size, the minimum size and the maximum size.
Window in WindowGroup
This is a code that defines a window with WindowGroup
.
import SwiftUI
@main
struct FrameSizeApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
In this code, the root view of the window is the ContentView
. SwiftUI set the window size to be fit to its contents. For example, in this sample code, the ContentView
is not defined its size, so the window size will be fit to the contents of the ContentView
.

Set the the default size, minimum size and maximum size of the root view
To set the default size, minimum size and maximum size of the ContentView
, use the frame()
modifier. The following code set the minimum size to 100px * 100px
, the default size size to 200px * 200px
and the maximum size to 300px * 300px
.
import SwiftUI
@main
struct FrameSizeApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.frame(minWidth: 100, idealWidth: 200, maxWidth: 300,
minHeight: 100, idealHeight: 200, maxHeight: 300,
alignment: .center)
}
}
}

The description of the arguments are as following table.
Argument | Description |
---|---|
minWidth | The minimum width of the window. |
idealWidth | The default width of the window. |
maxWidth | The maximum width of the window. |
minHeight | The minimum height of the window. |
idealHeight | The default height of the window. |
maxHeight | The maximum height of the window. |
alignment | The alignment for the root view. |
The alignment argument doesn’t specify the alignment of the window
Please pay attention to that the alighment
argument is not alignment of the window, it is a alignment of the root view in a window. The window is placed slightly above center in the screen. The vertical position is not center because the slightly above is easy to see than the center for the human.
Clear the saved frame of the window
SwiftUI save the window frame, and restore it when the app is relaunched. To clear the saved frame, run defaults
program in the terminal as follows.
defaults delete application-bundle-identifier
However, this command will remove the all user defaults of the specified application, so other settings will be also removed. If you would like to remove the window frame only, do as follows to look up the user defaults key that for saving the window frame, and remove the value for it. (In following example, the com.rk-k.FrameSize
is a bundle identifier. Replace it with your application’s bundle identifier.)
% defaults read com.rk-k.FrameSize
{
"NSWindow Frame SwiftUI.ModifiedContent<FrameSize.ContentView, SwiftUI._FlexFrameLayout>-1-AppWindow-1" = "140 1122 190 190 0 0 2560 1415 ";
}
% defaults delete com.rk-k.FrameSize "NSWindow Frame SwiftUI.ModifiedContent<FrameSize.ContentView, SwiftUI._FlexFrameLayout>-1-AppWindow-1"