TabView 内的 SwiftUI NavigationView 从堆栈中的任何视图弹回 Root
SwiftUI NavigationView inside TabView pops back to Root, from any view in the stack
在 TabView 中添加 NavigationView 时,我从堆栈中的任何视图中得到弹出到根目录的情况。
当然,预期的行为是每个视图都返回到前一个视图,直到我们到达根目录(主页)
(有一个功能性的解决方法,但我不太满意。)
所以我真的希望有人能指出问题的原因和可能的解决方案。
这是整个场景的精简代码示例。
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
NavigationView{ HomeView() }
.tabItem { Image(systemName: "house"); Text("Home") }
Text("Left").font(.title).fontWeight(.heavy)
.tabItem { Image(systemName: "chevron.left.square"); Text("Left") }
Text("Up").font(.title).fontWeight(.heavy)
.tabItem { Image(systemName: "chevron.up.square"); Text("Up") }
Text("Right").font(.title).fontWeight(.heavy)
.tabItem { Image(systemName: "chevron.right.square"); Text("Right") }
}
}
}
struct HomeView: View {
var body: some View {
Text("Home").font(.title).fontWeight(.heavy)
.navigationBarTitle("Home")
.navigationBarItems(trailing: NavigationLink(destination: NavChild1(), label: { Text("GoTo 1 >") }))
}
}
struct NavChild1: View {
var body: some View {
Text("Nav1").font(.title).foregroundColor(.white).background(Color.red)
.navigationBarTitle("Nav 1")
.navigationBarItems(trailing: NavigationLink(destination: NavChild2(), label: { Text("GoTo 2 >") }))
}
}
struct NavChild2: View {
var body: some View {
Text("Nav2").font(.title).foregroundColor(.white).background(Color.green)
.navigationTitle("Nav 2")
.navigationBarItems(trailing: NavigationLink(destination: NavChild3(), label: { Text("GoTo 3 >") }))
}
}
struct NavChild3: View {
var body: some View {
Text("Nav3").font(.title).foregroundColor(.white).background(Color.blue)
.navigationTitle("Nav 3")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
NavigationLink 似乎不能很好地与 .navigationBarItems(trailing:)
一起使用。
相反,您可以使用 toolbar
并在 .navigationBarTrailing
位置添加 ToolbarItem
:
struct HomeView: View {
var body: some View {
Text("Home").font(.title).fontWeight(.heavy)
.navigationTitle("Home")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
NavigationLink(destination: NavChild1(), label: { Text("GoTo 1 >") })
}
}
}
}
然后对其余链接执行相同的操作:
struct NavChild1: View {
var body: some View {
Text("Nav1").font(.title).foregroundColor(.white).background(Color.red)
.navigationTitle("Nav 1")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
NavigationLink(destination: NavChild2(), label: { Text("GoTo 2 >") })
}
}
}
}
struct NavChild2: View {
var body: some View {
Text("Nav2").font(.title).foregroundColor(.white).background(Color.green)
.navigationTitle("Nav 2")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
NavigationLink(destination: NavChild3(), label: { Text("GoTo 3 >") })
}
}
}
}
在 TabView 中添加 NavigationView 时,我从堆栈中的任何视图中得到弹出到根目录的情况。
当然,预期的行为是每个视图都返回到前一个视图,直到我们到达根目录(主页)
(有一个功能性的解决方法,但我不太满意。)
所以我真的希望有人能指出问题的原因和可能的解决方案。
这是整个场景的精简代码示例。
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
NavigationView{ HomeView() }
.tabItem { Image(systemName: "house"); Text("Home") }
Text("Left").font(.title).fontWeight(.heavy)
.tabItem { Image(systemName: "chevron.left.square"); Text("Left") }
Text("Up").font(.title).fontWeight(.heavy)
.tabItem { Image(systemName: "chevron.up.square"); Text("Up") }
Text("Right").font(.title).fontWeight(.heavy)
.tabItem { Image(systemName: "chevron.right.square"); Text("Right") }
}
}
}
struct HomeView: View {
var body: some View {
Text("Home").font(.title).fontWeight(.heavy)
.navigationBarTitle("Home")
.navigationBarItems(trailing: NavigationLink(destination: NavChild1(), label: { Text("GoTo 1 >") }))
}
}
struct NavChild1: View {
var body: some View {
Text("Nav1").font(.title).foregroundColor(.white).background(Color.red)
.navigationBarTitle("Nav 1")
.navigationBarItems(trailing: NavigationLink(destination: NavChild2(), label: { Text("GoTo 2 >") }))
}
}
struct NavChild2: View {
var body: some View {
Text("Nav2").font(.title).foregroundColor(.white).background(Color.green)
.navigationTitle("Nav 2")
.navigationBarItems(trailing: NavigationLink(destination: NavChild3(), label: { Text("GoTo 3 >") }))
}
}
struct NavChild3: View {
var body: some View {
Text("Nav3").font(.title).foregroundColor(.white).background(Color.blue)
.navigationTitle("Nav 3")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
NavigationLink 似乎不能很好地与 .navigationBarItems(trailing:)
一起使用。
相反,您可以使用 toolbar
并在 .navigationBarTrailing
位置添加 ToolbarItem
:
struct HomeView: View {
var body: some View {
Text("Home").font(.title).fontWeight(.heavy)
.navigationTitle("Home")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
NavigationLink(destination: NavChild1(), label: { Text("GoTo 1 >") })
}
}
}
}
然后对其余链接执行相同的操作:
struct NavChild1: View {
var body: some View {
Text("Nav1").font(.title).foregroundColor(.white).background(Color.red)
.navigationTitle("Nav 1")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
NavigationLink(destination: NavChild2(), label: { Text("GoTo 2 >") })
}
}
}
}
struct NavChild2: View {
var body: some View {
Text("Nav2").font(.title).foregroundColor(.white).background(Color.green)
.navigationTitle("Nav 2")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
NavigationLink(destination: NavChild3(), label: { Text("GoTo 3 >") })
}
}
}
}