Swift UI 需要同时保留 NavigationLink to detail view 和 Tap 手势识别器 [英] Swift UI need to keep both NavigationLink to detail view and Tap gesture recognizer

查看:41
本文介绍了Swift UI 需要同时保留 NavigationLink to detail view 和 Tap 手势识别器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一个简单的应用程序,它是一个包含项目的列表,它们会导致详细信息视图.我还有一个打开键盘的搜索栏,当用户点击键盘外的任何地方时,我需要隐藏键盘.

I am trying a simple app that is a List with items, they lead to detail view. I also have a search bar that opens keyboard, and I need to hide the keyboard when the user taps anywhere outside of the keyboard.

@State private var keyboardOpen: Bool = false

var body: some View {

    NavigationView {
        Form {
            Section {

                TextField("Search", text: $cityStore.searchTerm, onCommit: debouncedFetch)
                    .keyboardType(.namePhonePad)
                    .disableAutocorrection(true)        
                    .onTapGesture { self.keyboardOpen = true }
                    .onDisappear { self.keyboardOpen = false }
            }

            Section {
                List {
                    ForEach(cities) { city in
                        NavigationLink(
                        destination: DetailView(city: city)) {
                            VStack(alignment: .leading) {
                                Text("\(city.name)")
                            }
                        }
                    }
                }
            }
        }
        .navigationBarTitle("City list")
        .onTapGesture {
            if self.keyboardOpen {
                UIApplication.shared.endEditing()
                self.keyboardOpen = false
            }
        }
    }
}

您知道是否可以同时保持手势点击和跟随到详细信息视图?

Do you know if it's possible to keep both gesture tap and follow to detail view?

推荐答案

其实应该可以的,但不是.all GestureMask的bug.我向 Apple #FB7672055 提交了反馈,并建议每个受影响的人都这样做,越多越好.

Actually it should work, but it is not due to bug of .all GestureMask. I submitted feedback to Apple #FB7672055, and recommend to do the same for everybody affected, the more the better.

与此同时,这里有可能实现类似效果的替代方法/解决方法.

Meanwhile, here is possible alternate approach/workaround to achieve similar effect.

使用 Xcode 11.4/iOS 13.4 测试

Tested with Xcode 11.4 / iOS 13.4

extension UIApplication {  // just helper extension
    static func endEditing() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to:nil, from:nil, for:nil)
    }
}

struct TestEndEditingOnNavigate: View {
    @State private var cities = ["London", "Berlin", "New York"]
    @State private var searchTerm = ""
    @State private var tappedLink: String? = nil // track tapped link

    var body: some View {
        NavigationView {
            Form {
                Section {
                    TextField("Search", text: $searchTerm)
                }

                Section {
                    List {
                        ForEach(cities, id: \.self) { city in
                            self.link(for: city) // decompose for simplicity
                        }
                    }
                }
            }
            .navigationBarTitle("City list")
        }
    }

    private func link(for city: String) -> some View {
        let selection = Binding(get: { self.tappedLink }, // proxy bindng to inject...
            set: {
                UIApplication.endEditing()           // ... side effect on willSet
                self.tappedLink = $0
        })
        return NavigationLink(destination: Text("city: \(city)"), tag: city, selection: selection) {
            Text("\(city)")
        }
    }
}

这篇关于Swift UI 需要同时保留 NavigationLink to detail view 和 Tap 手势识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆