1
struct SheetView: View {
    @Binding var showSheetView: Bool
    @Environment(\.presentationMode) var mode

    var body: some View {
        NavigationView {
            Text("Sheet View content")
                .navigationBarTitle(Text("Sheet View"), displayMode: .inline)
                .navigationBarItems(trailing: Button(action: {
                    print("Dismissing sheet view...")
//                    self.mode.wrappedValue.dismiss()
//                    self.showSheetView.toggle()
                    self.showSheetView = false
                }) {
                    Text("Done").bold()
                })
        }
    }
}

struct ContentView2: View {
    @State var showSheetView = false

    var body: some View {
        NavigationView {
            Text("first")
        }.navigationBarTitle("asdf").navigationBarItems(trailing: Button(action: {
            self.showSheetView.toggle()
        }) {
            Text("Show Sheet View")
        }.sheet(isPresented: $showSheetView) {
            SheetView(showSheetView: self.$showSheetView)
        })
    }
}


struct ContentView: View {
    @State var showSheetView = false

    var body: some View {
        NavigationView {
            NavigationLink(destination: ContentView2()) {
                Text("ds")
            }
        }
    }
}

SheetView is presented successfully in ContentView2. And then i click Done button to dismiss and try to show it again. It will always fail. But when i place the launch button not in the navigation bar, the issue not exists.

1 Answer 1

1

It is known defect, but not with binding - with layout of navigation bar items...

Here is workaround

.navigationBarTitle("asdf").navigationBarItems(trailing: Button(action: {
    self.showSheetView.toggle()
}) {
    Text("Show Sheet View").padding()   // << add padding !!

You can find more details and alternate solution in SwiftUI modal presentation works only once from navigationBarItems

Sign up to request clarification or add additional context in comments.

1 Comment

It works. Using view debugger to address the issue is very smart.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.