Set the window size in SwiftUI

This article explains how to set the window default size, the minimum size and the maximum size.

TOC

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.

Created window size is fit to the ContentView.
Created window size is fit to 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)
        }
    }
}
Change the window size by code
Change the window size by code

The description of the arguments are as following table.

ArgumentDescription
minWidthThe minimum width of the window.
idealWidthThe default width of the window.
maxWidthThe maximum width of the window.
minHeightThe minimum height of the window.
idealHeightThe default height of the window.
maxHeightThe maximum height of the window.
alignmentThe alignment for the root view.
The arguments of the frame modifier

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"
Let's share this post !

Author of this article

Akira Hayashiのアバター Akira Hayashi Representative, Software Engineer

I am an application developer loves programming. This blog is a tech blog, its articles are learning notes. In my work, I mainly focus on desktop and mobile application development, but I also write technical books and teach seminars. The websites of my work and books are here -> RK Kaihatsu.

TOC