macosswiftuiwindowwindow-resize

Unable to resize sheet on macOS 15 with SwiftUI


In my macOS app, I can extend the main window of the app like that:

Window("SingleWindow", id: "single") {
   RootMacView()
     .frame(minWidth: 1000, maxWidth: .infinity, minHeight: 800, maxHeight: .infinity)
}

It works and I can use the arrows at the border of the window to extend it.

However, I didn't find the way to do the same thing with a sheet.

.sheet(isPresented: $showCellarsEditor) {
    VStack {
        Text("Test")
    }
    .frame(minWidth: 1000, maxWidth: .infinity, minHeight: 800, maxHeight: .infinity)    
}

The arrows never appear on the sides of the sheet.

What did I miss ? It's not possible to extend sheets on macOS ?

Thanks !


Solution

  • Use the .fitted presentation sizing behaviour.

    On macOS, presentations with .fitted sizing are user-resizable by default.

    Also:

    The presentation sizing is dictated by the ideal size of the content

    So the initial size of the sheet will be the ideal size. You should pass in idealWidth: and idealHeight: too, otherwise the sheet will expand to the max size.

    .sheet(isPresented: .constant(true)) {
        VStack {
            Text("Some Text")
        }
        .frame(minWidth: 400, idealWidth: 400, maxWidth: .infinity, minHeight: 400, idealHeight: 400, maxHeight: .infinity)
        .presentationSizing(.fitted)
    }
    

    On older versions, you can introspect the window for the sheet and add the .resizable style mask, but this will not respect the min/max sizes you set in SwiftUI.

    .sheet(isPresented: .constant(true)) {
        VStack {
            Text("Some Text")
        }
        .frame(minWidth: 400, minHeight: 400)
        // suppose you want to support macOS 14
        .introspect(.window, on: .macOS(.v14)) { window in
            window.styleMask.formUnion(.resizable)
        }
    }
    

    Then you can use something like this to pick which version to run.