SwiftUI列表onTapGesture涵盖了NavigationLink

时间:2020-08-15 05:38:43

标签: swift swiftui swiftui-navigationlink

我想在点击列表背景时隐藏键盘,但是onTapGesture将涵盖NavigationLink。这是错误还是有更好的解决方案?

struct ContentView: View {
    @State var text: String = ""
    
    var body: some View {
        NavigationView {
            List {
                NavigationLink("NextPage", destination: Text("Page"))
                
                TextField("Placeholder", text: $text)
            }
            .onTapGesture {
                
                // hide keyboard...
                UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
            }
        }
    }
}

谢谢!

更新

感谢asperi,我发现了另一种方法:只需将其放在节标题中即可。至于样式,我们应该为NavigationLink创建一个自定义的ButtonStyle。

这里是使用InsetGroupedListStyle的示例。

struct ContentView: View {
    @State var text: String = ""
    
    var body: some View {
        NavigationView {
            List {
                Section(
                    header: NavigationLink(destination: Text("Page")) {
                        HStack {
                            Text("NextPage")
                                .font(.body)
                                .foregroundColor(Color.primary)
                            
                            Spacer()
                            
                            Image(systemName: "chevron.forward")
                                .imageScale(.large)
                                .font(Font.caption2.bold())
                                .foregroundColor(Color(UIColor.tertiaryLabel))
                            
                        }
                        .padding(.vertical, 12)
                        .padding(.horizontal)
                    }
                    .textCase(nil)
                    .buttonStyle(CellButtonStyle())
                    .clipShape(RoundedRectangle(cornerRadius: 10))
                    .padding(.horizontal, -16)
                ) {}
                
                TextField("Placeholder", text: $text)
                
            }.listStyle(InsetGroupedListStyle())
            .onTapGesture {
                
                // hide keyboard...
                UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
            }
        }
    }
}

struct CellButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .background(
                configuration.isPressed
                    ? Color(UIColor.systemGray5)
                    : Color(UIColor.secondarySystemGroupedBackground)
            )
    }
}

1 个答案:

答案 0 :(得分:0)

这是解决此问题的可能方向-通过同时处理所有水龙头并以编程方式导航。在Xcode 12 / iOS 14上进行了测试。

demo

truct ContentView: View {
    @State var text: String = ""

    var body: some View {
        NavigationView {
            List {
                MyRowView()
                TextField("Placeholder", text: $text)
            }
            .simultaneousGesture(TapGesture().onEnded {

                // hide keyboard...
                UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
            })
        }
    }
}

struct MyRowView: View {
    @State private var isActive = false
    var body: some View {
        NavigationLink("NextPage", destination: Text("Page"), isActive: $isActive)
            .contentShape(Rectangle())
            .onTapGesture {
                DispatchQueue.main.async { // maybe even with some delay
                    self.isActive = true
                }
            }
    }
}