0

I am having a lot of trouble with getting a .sheet() to work and I am beginning to wonder why I want to use one. What are the advantages of .sheet() and .popover() over e.g. changing the zIndex?

I have a search window in my app which popped up and down happily one or two betas ago. Now it pops up and down once. The code I am using works happily when isolated but not in my app. I tried using both the isPresented and item forms but neither were successful. The attached code works though if I have made any errors I would like to know. It is more complex than it needs to be as I was grasping at straws trying to find the problem.

struct ContentView : View {
    @State var count = 0
    @State var dismisses = 0

    var body: some View {
        VStack {
            Spacer()
            ShowButton(count: $count, dismisses: $dismisses)
            Spacer()
            Text("Shows: \(count), Dismisses:\(dismisses)")
         }
    }
}

struct ShowButton: View {
    @State var show = false
    @Binding var count: Int
    @Binding var dismisses: Int

    var body: some View {
        Button(
            action: { self.count += 1; self.show = true },
            label: { Text("Show sheet") }
        )
        .sheet(
            isPresented: $show,
            onDismiss: { self.dismisses += 1 },
            content: { Show(text: "Test #\(self.count)") }
        )
    }
}

struct Show: View {
    let text: String
    @Environment(\.presentationMode) var mode: Binding<PresentationMode>

    var body: some View {
        VStack {
            Text(text)
            Button(
                action: {
                    self.mode.value.dismiss()
                },
                label: { Text("Dismiss") }
            )
        }
    }
}

This code works as expected, similar code in my app works once.

Are there any good alternatives to sheet()/popover()?

Michael Salmon
  • 1,056
  • 7
  • 15
  • The problem is it's hard to say what's wrong with working code. We need the code which doesn't work. :) – Timur Bernikovich Aug 08 '19 at 12:07
  • 2
    There is a bug in SwiftUI where if you put your `.sheet` inside of a `ForEach` (or possibly even a `ScrollView`), the sheet will only ever get triggered once. In your production code, try moving your `.sheet`. https://stackoverflow.com/a/57279307/3179416 – Zain Aug 08 '19 at 13:40
  • 1
    Thanks, commenting out the ScrollView fixed the problem, now I just have to find a place outside of the ScrollView to stick it. – Michael Salmon Aug 08 '19 at 13:46
  • @TimurBernikovich, I am close to 3000 lines now and I didn't think that anyone would want me to post that. It is at https://github.com/msalmonse/ColourViewer, the offending sheet is in ColourSearch and the sheet is in ColourSearchShow – Michael Salmon Aug 08 '19 at 13:54

0 Answers0