Default window size does not work for macOS 13 app using SwiftUI

I'm using Xcode 14.1 and created a new macOS 13 project. Before I build/run the project, I added a .defaultSize to the window group. But when I run the project for the first time, the window size does not adhere to the default size. Is there something else needed to define a default window size?

Here is the ContentView.swift file:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
        }
        .padding()
    }
}

Here is the ExampleApp.swift file:

import SwiftUI

@main
struct WindowDefaultSizeApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .defaultSize(width: 300, height: 200)
    }
}
Answered by rgreene62 in 745352022

It does work, but only if you haven't run the app previously, such that the system "remembers" a non-default size that was used then, or if you manage to reset that memory.
There may be a simpler way to do that, but so far the only reliable way I've found is to:

  1. delete the reference to my app in ~/Library/Application\ Support/com.apple.windowmanager/state.plist and resave it
  2. delete my app's folder in ~/Library/Containers/ (that's for a sandboxed app, it may otherwise be under Preferences)
  3. empty the trash
  4. restart the Mac

Also, so far I've only tested this with the Window scene rather than WindowGroup. But it was apparently not working with Window either till I managed to clear the previous non-default size using the steps above.

This article mentions that .defaultSize doesn’t work so maybe it is a bug. https://troz.net/post/2022/swiftui-mac-2022/

Accepted Answer

It does work, but only if you haven't run the app previously, such that the system "remembers" a non-default size that was used then, or if you manage to reset that memory.
There may be a simpler way to do that, but so far the only reliable way I've found is to:

  1. delete the reference to my app in ~/Library/Application\ Support/com.apple.windowmanager/state.plist and resave it
  2. delete my app's folder in ~/Library/Containers/ (that's for a sandboxed app, it may otherwise be under Preferences)
  3. empty the trash
  4. restart the Mac

Also, so far I've only tested this with the Window scene rather than WindowGroup. But it was apparently not working with Window either till I managed to clear the previous non-default size using the steps above.

Default window size does not work for macOS 13 app using SwiftUI
 
 
Q