Showing sheet dismiss navigation view child

I have a problem with SwiftUI and tabview and the problem is when I'm on a second navigation page it's back to the first navigation when I need to open a sheet. Tab1->FirstView->Secondview->Sheet after the sheet opened the navigation view behind the scene switched to first navigation!



import SwiftUI

struct ContentView: View {

	var body: some View {

		NavigationView{

			TabView{

				Tab1()

					.tabItem({Text("tab1")})

				Tab2()

					.tabItem({Text("tab2")})

			}

		}

	}

}



struct Tab1: View {

	@State
	var shownextPage = false

	var body: some View {

		ZStack{

			Button ("Next Page"){

				shownextPage = true

			}

			NavigationLink( destination: SecondView(), isActive: $shownextPage) {

				EmptyView()

			}

		}

	}

}



struct Tab2: View {

	var body: some View {

		Text("Tab 2 detail")
	}

}

struct SecondView: View {

	@State
	var showSheet:Bool = false

	var body: some View {

		VStack{

			Button {

				showSheet = true

			} label: {

				Text("open Sheet")

					.padding()

			}

			.sheet(isPresented: $showSheet, onDismiss: nil) {

				Text("hello in sheet")

			}

		}

	}

}

struct ContentView_Previews: PreviewProvider {

	static var previews: some View {

		ContentView()

	}

}

I have the same problem.


When I commented out the top “TabView”, the problem disappeared.

I've found a solution for that but I truly believe that it's a SwiftUI bug. You must move the navigation link to the upper parent and view.

import SwiftUI

struct ContentView: View {

    @State
    var shownextPage = false

    var body: some View {

        NavigationView{
            ZStack{
                TabView{
                    Tab1(shownextPage: $shownextPage)
                        .tabItem({Text("tab1")})
                    Tab2()
                        .tabItem({Text("tab2")})
                }

                NavigationLink(destination: SecondView(), isActive: $shownextPage) {
                    EmptyView()
                }
            }
            .navigationViewStyle(.stack)
        }
    }
}

struct Tab1: View {

    @Binding
    var shownextPage:Bool

    var body: some View {

        ZStack{
            Button ("Next Page"){
                shownextPage = true
            }
        }
    }
}

struct Tab2: View {

    var body: some View {
        Text("Tab 2 detail")
    }
}

struct SecondView: View {

    @State
    var showSheet:Bool = false

    var body: some View {

        VStack{

            Button {
                showSheet = true
            } label: {
                Text("open Sheet")
                    .padding()
            }
        }
        .sheet(isPresented: $showSheet, onDismiss: nil) {
            Text("hello in sheet")
        }
    }
}

struct ContentView_Previews: PreviewProvider {

    static var previews: some View {
        ContentView()
    }
}

There is a mistake. You need to move your NavigationView into Tab1. Also remove the ZStack.

struct Tab1: View {

    @State var shownextPage = false

    var body: some View {
        NavigationView{
            NavigationLink( destination: SecondView(), isActive: $shownextPage) {
                Text("Next Page")
            }
        }
    }
}

try this "fullScreenCover" instead of "sheet"

Showing sheet dismiss navigation view child
 
 
Q