1

Consider this code that presents a sheet on macOS:

struct ContentView: View {
    @State var showSheet = false
    var body: some View {
        VStack {
            Text("Hello, world!").padding()
            Button("Show sheet") {
                showSheet = true
            }
        }
        .sheet(isPresented: $showSheet) {
            SheetView()
        }
    }
}
struct SheetView: View {
    @State var greenBiggerBox = false
    var body: some View {
        VStack {
            Toggle("Show bigger box", isOn: $greenBiggerBox)
            if greenBiggerBox {
                Rectangle().frame(width: 640, height: 480).foregroundColor(.green)
            } else {
                Rectangle().frame(width: 320, height: 240).foregroundColor(.red)
            }
        }.padding()
    }
}

The problem: initially, the sheet is presented, sized correctly to its contents (good). However, when something happens inside the sheet that resizes the content (like the toggle in the sheet), the sheet window is not resized, yielding a sheet size that is either too large or too small.

How do I make the sheet resize correctly and automatically if its the size of its content changes?

Jaanus
  • 17,688
  • 15
  • 65
  • 110
  • Works correctly with Xcode 11.4 / macOS 10.15.6. Cannot test on Big-Sur. – Asperi Aug 16 '20 at 08:58
  • Also good with Xcode 12b3/ macOS 10.15.6 – Asperi Aug 16 '20 at 09:06
  • Interesting. It works correctly in Catalina indeed, and doesn’t work correctly on Big Sur. – Jaanus Aug 16 '20 at 12:17
  • This is still an issue, it only works because you set the frame as such `.frame(width: 640, height: 480)` on the rectangles, but if you add elements such as `Text("some text")` wider than the initial view, it doesn't resize properly. See https://stackoverflow.com/questions/76525061/swiftui-sheet-view-resizing-issue-testing-on-macos – LostBalloon Jun 21 '23 at 17:56

0 Answers0