Search code examples
iosswiftswiftuiswiftui-listswiftui-navigationlink

SwiftUI TapGesture Not Working with NavigationLink in List on iOS


When I use .simultaneousGesture(TapGesture().onEnded{ //code to run }) on a NavigationLink in SwiftUI for iOS, the code runs fine and the link opens.

However, if the NavigationLink is in a List, the code runs but the link does not open.

Is there another way I can run some code after the List NavigationLink is tapped, but before the TextEditor appears?

Using .onAppear on the TextEditor, is not suitable because I need the code to run before the TextEditor appears - partly due to a bug with TextEditor's re-rendering and partly to keep things flowing in the right order.

Code to replicate the issue:

import SwiftUI

struct ContentView: View {
    @State private var textString = ""
    @State private var listMembers = ["Link A", "Link B", "Link C"]
    var body: some View {
        NavigationView {
            VStack {
                List(listMembers, id: \.self) { member in
                    NavigationLink(member, destination: TextEditor(text: $textString))
                        .simultaneousGesture(TapGesture().onEnded {
                            textString = "Link A, B, or C was tapped."
                            print(textString)
                    })
                }
                NavigationLink("Link D", destination: TextEditor(text: $textString))
                    .simultaneousGesture(TapGesture().onEnded {
                        textString = "Link D was tapped."
                    })
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Thank You!


Solution

  • The best I could come up with is using ForEach instead of List. It requires more formatting to make it look nice, but it seems to solve the problem.

    import SwiftUI
    
    struct ContentView: View {
        @State private var textString = ""
        @State private var listMembers = ["Link A", "Link B", "Link C"]
        var body: some View {
            NavigationView {
                VStack {
                    /*List(listMembers, id: \.self) { member in
                        NavigationLink(member, destination: TextEditor(text: $textString))
                            .simultaneousGesture(TapGesture().onEnded {
                                textString = "Link A, B, or C was tapped."
                                print(textString)
                        })
                    }*/
                    ForEach(listMembers, id: \.self) { member in
                        NavigationLink(member, destination: TextEditor(text: $textString))
                            .simultaneousGesture(TapGesture().onEnded {
                                textString = "Link A, B, or C was tapped."
                                print(textString)
                        })
                    }
                    NavigationLink("Link D", destination: TextEditor(text: $textString))
                        .simultaneousGesture(TapGesture().onEnded {
                            textString = "Link D was tapped."
                        })
                }
            }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }