SWIFTUI: Can't dismiss Sheet after changing ScreenSize classes

Viewed 178

I toggle a sheet in SwiftUI with the following Button

Button(action: {
                    self.statusPopoverIsShown.toggle()
                })

So the following sheet appears

.sheet(isPresented: self.$popoverIsShown) {
                    RandomSheet(popoverIsShown: self.$popoverIsShown)
}

Then I have a button inside the RandomSheetto dismiss the sheet (sets the popoverIsShown to false). Everything works fine.

But when I start using the app in splitscreen or somehow change the sizeclass SwiftUI transforms the sheet to a fullscreen iPhone-like sheet and the dismiss button/the binding does not work anymore.

Is there any solution to avoid this and keep the binding stable?

1 Answers

The following works with any size class changes. Tested with Xcode 12 / iOS 14

struct TestSheet: View {
    @State private var popoverIsShown = false
    var body: some View {
        Button("Show Sheet") {
            self.popoverIsShown = true
        }
        .sheet(isPresented: self.$popoverIsShown) {
            RandomSheet(popoverIsShown: self.$popoverIsShown)
        }
    }
}

struct RandomSheet: View {
    @Binding var popoverIsShown: Bool
    var body: some View {
        Button("Close") { self.popoverIsShown = false }
    }
}
Related