'How to set a default/preferred window size for a Mac Catalyst app?

I have an iOS app that I have enabled Catalyst for. One function in the app opens a new window. By default, this window opens very large but I need a way to make it smaller by default. I know you can set windowScene.sizeRestrictions?.minimumSize and .maximumSize, but that then limits the window to my preferred size. I'd like to make it so the window opens a certain size, say 500x800 by default, but can be expanded by the user to whatever they want.

I have tried window?.frame = CGRect(origin: .zero, size: CGSize(width: 500, height: 800)) in the SceneDelegate, but it has no effect.

Visual example: Image showing my goal window size versus the default window size



Solution 1:[1]

Following Dylan's great tip,

func tidyCatalystWindow() {
    #if targetEnvironment(macCatalyst)
    UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
     .forEach { ws in
        ws.sizeRestrictions?.minimumSize = CGSize(width: 500, height:800)
        ws.sizeRestrictions?.maximumSize = CGSize(width: 500, height: 800)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            ws.sizeRestrictions?.maximumSize = CGSize(width: 9000, height: 9000)
        }
    }
    #endif
}

It's that easy.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1