Hey, I am developing my app in Swift using Xcode for iOS 16 or later. I want to implement the navigation behavior found in apps like WhatsApp or Instagram, where navigating from the feed to a user's profile keeps the tab bar visible. Then, when opening a chat from the profile, a new view appears, leaving the profile view behind along with the tab bar.
I have this code as a base to achieve this, but when navigating from View 1 to View 2, it applies the effect that should only happen in View 3. I haven't been able to prevent View 2 from using this effect and limit it only to View 3. Can anyone help me???
import SwiftUI
struct ContentView: View {
@State private var path: [String] = [] // Controls the navigation stack in View1
var body: some View {
NavigationStack(path: $path) {
TabView {
View1(path: $path)
.tabItem {
Label("View 1", systemImage: "1.circle")
}
View4()
.tabItem {
Label("View 4", systemImage: "4.circle")
}
}
.navigationDestination(for: String.self) { value in
if value == "View3" {
View3(path: $path) // View3 outside the TabView
}
}
}
}
}
struct View1: View {
@Binding var path: [String]
var body: some View {
VStack {
Text("View 1 with TabBar")
Button("Go to View 2") {
path.append("View2") // Adds View2 to the stack
}
}
.navigationDestination(for: String.self) { value in
if value == "View2" {
View2(path: $path)
}
}
}
}
struct View2: View {
@Binding var path: [String]
var body: some View {
VStack {
Text("View 2 with TabBar")
Button("Go to View 3 (Without TabBar)") {
path.append("View3") // Adds View3 to the stack
}
}
}
}
struct View3: View {
@Binding var path: [String]
var body: some View {
VStack {
Text("View 3 without TabBar")
.font(.largeTitle)
.padding()
Button("Go Back") {
path.removeLast() // Returns to View2
}
}
}
}
struct View4: View {
var body: some View {
Text("View 4 with TabBar")
}
}