At the bottom of this page Paul gives a tip.
Tip: It’s common to want to use NavigationView and TabView at the same time, but you should be careful: TabView should be the parent view, with the tabs inside it having a NavigationView as necessary, rather than the other way around.
Although, it is a bit difficult to tell exactly what that means.
Should we wrap all of our tabs in a single NavigationView
?
struct ContentView: View {
@State private var selectedTab = "One"
var body: some View {
TabView(selection: $selectedTab) {
NavigationView {
Text("Tab 1")
.tabItem {
Label("One", systemImage: "star")
}
.tag("One")
Text("Tab 2")
.tabItem {
Label("Two", systemImage: "circle")
}
.tag("Two")
}
}
}
}
Or does each tab need its own NavigationView
?
struct ContentView: View {
@State private var selectedTab = "One"
var body: some View {
TabView(selection: $selectedTab) {
NavigationView {
Text("Tab 1")
}
.tabItem {
Label("One", systemImage: "star")
}
.tag("One")
NavigationView {
Text("Tab 2")
}
.tabItem {
Label("Two", systemImage: "circle")
}
.tag("Two")
}
}
}
I believe the second option is the correct way. However, if you are using your own custom Views instead of just Text Views, you could just wrap the entire body of the View that you have created in a NavigationView
, and then you wouldn't need to worry about wrapping it in a NavigationView
again when you are creating the TabView
.