0

I have a list item with some text stuff, navigationLink and button to show .sheet.When I click on either navigation link or show sheet button both navigation destination and the sheet appear.How to avoid this behaviour?

Note: This is minimal produceable code.

struct ContentView: View {
    @State var shouldSheetShow:Bool = false;

    var body: some View {
        NavigationView{
            List{
                VStack(alignment: .leading){
                    Text("Some Text Stuff")
                    NavigationLink(
                        destination: Text("navigation link"),
                        label: {
                            Text("Navigate To some view")
                                .background(Color.green)
                        })

                    Button(action: {
                        self.shouldSheetShow = true
                    }, label: {
                        HStack{
                            Text("Show sheet")
                        }
                        .background(Color.blue)
                        .sheet(isPresented: $shouldSheetShow, content: {
                            Text("sheet")
                        })

                    })
                }
                .frame(width: 300, height: 150, alignment: .center)
                .background(Color.gray)
            }

        }
    }
}

List Item

1 Answer 1

3

A possible solution is to replace the Button with Text plus an onTapGesture

Replace

Button(action: {
        self.shouldSheetShow = true
    }, label: {
        HStack{
            Text("Show sheet")
        }
        .background(Color.blue)
        .sheet(isPresented: $shouldSheetShow, content: {
            Text("sheet")
        })

    })

with

Text("Show sheet")
        .background(Color.blue)
        .sheet(isPresented: $shouldSheetShow, content: {
            Text("sheet")
        })
        .onTapGesture {
            self.shouldSheetShow = true
        }
Sign up to request clarification or add additional context in comments.

2 Comments

It works, can you please explain the theoretical reason to that issue? I mean why we cannot use my combination inside list item.because my combination working properly when it is not inside a list.
Honestly I don't know the proper reason but I guess that particularly in a List the tappable area of the NavigationItem is extended to the whole cell and affects also the Button area.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.