jonthornham
jonthornham

Reputation: 3335

onTapGesture To Dismiss Keyboard Makes View Inoperable

I am working on a SwiftUI app. In the app I have a custom coded List that acts as a Form. The reason it is custom coded is because I am using a custom color. I have TextField rows among other rows that act as Navigation Links. My issue is that when I add a onTapGesture to dismiss the keyboard all other row functions stop working. For example the NavigationLinks.

NavigationLink Work Here

ZStack(alignment: .leading, content: {
            Color.pacificBlue
                .edgesIgnoringSafeArea(.all)
            List {
                Section(header: Text("Header") {
                    NavigationLink(
                        destination: SecondaryView(),
                        label: {
                            Text("Secondary View")
                        })
                    TextField("MyField", text: self.$myField)
                }
             }
}

NavigationLink Does Not Work Here

    ZStack(alignment: .leading, content: {
                Color.pacificBlue
                    .edgesIgnoringSafeArea(.all)
                List {
                    Section(header: Text("Header") {
                        NavigationLink(
                            destination: SecondaryView(),
                            label: {
                                Text("Secondary View")
                            })
                        TextField("MyField", text: self.$myField)
                    }
                 }
    }
.onTapGesture {
  self.dismissKeyboard()
}

Dismiss Keyboard

extension View {
    func dismissKeyboard() {
        let resign = #selector(UIResponder.resignFirstResponder)
        UIApplication.shared.sendAction(resign, to: nil, from: nil, for: nil)
    }
}

Is there a way to work around this without having to move to a secondary view to add text. Any help would be appreciated.

Upvotes: 0

Views: 240

Answers (1)

Taking the answer from: SwiftUI NavigationLink in list

the following works for me for NavigationLink. If you have a Button for example, this will not work.

struct ContentView: View {
    @State var myField = ""
    @State private var showIt: Int? = 0  // <-- here
    
    var body: some View {
        NavigationView {
            ZStack(alignment: .leading) {
                Color.blue.edgesIgnoringSafeArea(.all)
                List {
                    Section(header: Text("Header")) {
                        // -- here --
                        NavigationLink(destination: Text("destination view"), tag: 1, selection: $showIt) {
                            Text("Secondary View")
                        }.disabled(true)    // <-- here
                        .onTapGesture { showIt = 1 }  // <-- here
                        TextField("MyField", text: $myField)
                    }
                }.listStyle(.plain)
            }
            .onTapGesture {
                self.dismissKeyboard()
            }
        }.navigationViewStyle(.stack)
    }
}

Upvotes: 0

Related Questions