2

我想ScrollViewNavigationLink. List但是,点击ScrollView没有被 a 注册NavigationLink,因此不会导航到目的地。

NavigationView {
    List {
        NavigationLink(destination: Text("Detail")) {
            ScrollView(.horizontal) {
                Text("Tapping here does not navigate.")
            }
        }
    }
}

关于我们如何防止ScrollView捕获导航点击手势的任何想法?

4

2 回答 2

2

您可以移动NavigationLinkbackground并在以下位置激活它onTapGesture

struct ContentView: View {
    @State var isLinkActive = false
    
    var body: some View {
        NavigationView {
            List {
                ScrollView(.horizontal) {
                    Text("Tapping here does not navigate.")
                }
                .onTapGesture {
                    isLinkActive = true
                }
            }
            .background(
                NavigationLink(destination: Text("Detail"), isActive: $isLinkActive) {}
            )
        }
    }
}
于 2020-10-26T23:09:36.843 回答
1

最终目标尚不清楚,但以下替代方案也确实有效(使用 Xcode 12 / iOS 14 测试)

NavigationView {
    List {
        ScrollView(.horizontal) {
            NavigationLink(destination: Text("Detail")) {
                Text("Tapping here does not navigate.")
            }
        }
    }
}
于 2020-10-27T04:19:51.317 回答