SwiftUI 列表 onTapGesture 覆盖了 NavigationLink

标签 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)
            }
        }
    }
}

谢谢!

update

感谢 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)
            )
    }
}

最佳答案

这是解决此问题的一个可能的方向 - 通过同时处理所有点击并以编程方式导航。使用 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
                }
            }
    }
}

关于SwiftUI 列表 onTapGesture 覆盖了 NavigationLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63422953/

相关文章:

swift - 变量引用的实例到底在哪里?

ios - `<Value>` 中的 `enum Result<Value> { ... }` 是什么?

ios - 带有 UIRefreshControl 的 TableView 在重新加载时似乎有点故障?

swift - 如果列表顺序更改,SwiftUI NavigationView 中的选择会丢失

ios - 如何相应地操作解析数据?

ios - SwiftUI:动画文本颜色 - foregroundColor()

Swiftui iOS 14 工具栏

ios - 导航链接仅在 SwiftUI 中有效一次

ios - 使用 NavigationLink 打开新 View 时如何隐藏 TabView?

xcode - macOS SwiftUI : MenuItem to open default browser to a URL?