我无法在列表的同一行中使用多个 NavigationLink。
看起来导航堆栈完全混乱了,因为你点击一次,它就会进入多个视图并不稳定地返回......
在 TestList 中,我尝试在部分中添加单独的 NavigationLinks,并且尝试将 NavigationLinks 移动到视图层次结构中的两个不同位置...
我尝试为列表的每一行添加两个 NavigationView,但是
然后当我需要它时,navigationTitleBar不会消失..
struct ContentView: View {
var body: some View {
NavigationView {
TestList()
}
}
}
struct TestList: View {
var body: some View {
List {
ListCellView()
}
}
}
struct ListCellView: View {
var body: some View {
VStack {
Spacer()
NavigationLink(destination: TestDestination1()) {
Text("Test Destination 1")
.frame(width: 140, height: 50)
.background(RoundedRectangle(cornerRadius: 7.0).strokeBorder(Color.green, lineWidth: 3.0))
}
Spacer()
NavigationLink(destination: TestDestination2()) {
Text("Test Destination 2")
.frame(width:140, height: 50)
.background(RoundedRectangle(cornerRadius: 7.0).strokeBorder(Color.purple, lineWidth: 3.0))
Spacer()
}
}
}
}
struct TestDestination1: View {
var body: some View {
Text("Test Destination 1")
}
}
struct TestDestination2: View {
var body: some View {
Text("Test Destination 2")
}
}
我希望当您点击 NavigationLink 时,它将导航到目标视图。
发生的情况是,当两个 NavigationLink 位于列表的同一行并且您点击它时,它将:
1. 转到其中一个视图
2. 点击“返回”后,它将带您返回到该视图,然后带您到另一个目标视图。
https://youtu.be/NCTnqjzJ4VE
正如其他人提到的,为什么 1 个单元格中有 2 个导航链接。问题在于 Cell 的多个按钮和手势。我猜想每个单元格最多有 1 个 Button/NavigationLink。正如您所注意到的,在您的视频中,您点击了一个 NavigationLink,但您的整个单元格得到了该手势(突出显示),这反过来又影响了其他按钮/NavigationLink。
不管怎样,你可以让它工作,1 个单元格中的 2 个导航链接,只需一个 hack。下面我创建了 SGNavigationLink,我将其用于我自己的应用程序来解决您的问题。它只是取代了NavigationLink并基于TapGesture,所以你会失去亮点。
注意:我稍微修改了您的 ListCellView,因为我的 SGNavigationLink 中的 Spacer 造成了内部崩溃。
struct ListCellView: View {
var body: some View {
VStack {
HStack{
SGNavigationLink(destination: TestDestination1()) {
Text("Test Destination 1")
.frame(width: 140, height: 50)
.background(RoundedRectangle(cornerRadius: 7.0).strokeBorder(Color.green, lineWidth: 3.0))
}
Spacer()
}
HStack{
SGNavigationLink(destination: TestDestination2()) {
Text("Test Destination 2")
.frame(width:140, height: 50)
.background(RoundedRectangle(cornerRadius: 7.0).strokeBorder(Color.purple, lineWidth: 3.0))
}
Spacer()
}
}
}
}
struct SGNavigationLink<Content, Destination>: View where Destination: View, Content: View {
let destination:Destination?
let content: () -> Content
@State private var isLinkActive:Bool = false
init(destination: Destination, title: String = "", @ViewBuilder content: @escaping () -> Content) {
self.content = content
self.destination = destination
}
var body: some View {
return ZStack (alignment: .leading){
if self.isLinkActive{
NavigationLink(destination: destination, isActive: $isLinkActive){Color.clear}.frame(height:0)
}
content()
}
.onTapGesture {
self.pushHiddenNavLink()
}
}
func pushHiddenNavLink(){
self.isLinkActive = true
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)