Fixed SwiftUI window sizes in macOS with Xcode 14 and Ventura for macOS 11/12 targets

I have a project that requires a fixed window size. In previous versions of Xcode when I compile the project with the following :

struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .frame(width: 800, height: 600)
        }
    }
}

It will result in a window with a fixed size of 800x600 (i.e. the window inherits the fixed content size)

With Xcode 14 and macOS Ventura, this is no longer the case. The above code compiles but now the window is resizable, with the content inhabiting an 800x600 area.

In Ventura there is the   .windowResizability(.contentSize) property but this is macOS 13 only. I need my app to be able to support macOS 11 and 12 and now the window behaviour is broken with the latest Xcode.

Is there any way to get around this that doesn't involve running a seperate macOS 12/Xcode 13 environment?

Answered by martin in 735997022

You can make the .windowResizability(.contentSize) scene modifier conditional like so:

extension Scene {
    func windowResizabilityContentSize() -> some Scene {
        if #available(macOS 13.0, *) {
            return windowResizability(.contentSize)
        } else {
            return self
        }
    }
}

And then use it on WindowGroup this way:

WindowGroup {
    ...
}
.windowResizabilityContentSize()
Accepted Answer

You can make the .windowResizability(.contentSize) scene modifier conditional like so:

extension Scene {
    func windowResizabilityContentSize() -> some Scene {
        if #available(macOS 13.0, *) {
            return windowResizability(.contentSize)
        } else {
            return self
        }
    }
}

And then use it on WindowGroup this way:

WindowGroup {
    ...
}
.windowResizabilityContentSize()

You rock - have been looking for this for way too long ... .windowResizability(.contentSize) fixed my issues !

+1 for those comments that "windowResizability" doesn't answer the question. The question still stands: How can a window be specified to be NOT resizable in macOS 11 and 12 ?

Fixed SwiftUI window sizes in macOS with Xcode 14 and Ventura for macOS 11/12 targets
 
 
Q