SwiftUI - OnExitCommand inside TabView

Markus Moltke

I have lately been trying to make a tvOS app, but have run into the following rather annoying problem. I can't use navigation inside a TabView and still have the menu button on the remove take me back to the previous state.

struct TestView: View {
    
    @State var selection : Int = 0
    
    var body: some View {
        TabView(selection: self.$selection) {
            ExpView()
            .tabItem {
                HStack {
                    Image(systemName: "magnifyingglass")
                    Text("Explore")
                }
            }
            .tag(0)
        }
    }
}

struct ExpView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: DetailView(title: "Hey")) {
                Text("Detail")
            }
        }
    }
}

struct DetailView: View {
    var title : String
    var body: some View {
        VStack {
            Text(title)
        }
    }
}

My question is: Is there any way to enable the menu button to go back to the previous view in the hierachy without dismissing the app completely?

Markus Moltke

So I found a workaround for the issue. If you place the navigationView outside the TabView and then use the following code it works:

struct TestView: View {
    
    @State var selection : Int = 0
    @State var hideNavigationBar : Bool
    
    var body: some View {
        NavigationView {
            TabView(selection: self.$selection) {
                ExpView(hideNavigationBar: self.$hideNavigationBar)
                .tabItem {
                    HStack {
                        Image(systemName: "magnifyingglass")
                        Text("Explore")
                    }
                }
                .tag(0)
            }
        }
    }
}

struct ExpView: View {
    
    @Binding var hideNavigationBar : Bool
    
    var body: some View {
        NavigationLink(destination: DetailView(title: "Hey")) {
            Text("Detail")
        }.navigationBarTitle("")
        .navigationBarHidden(self.hideNavigationBar)
        .onAppear {
            self.hideNavigationBar = true
        }
    }
}

struct DetailView: View {
    var title : String
    var body: some View {
        VStack {
            Text(title)
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related