Search code examples
iosswiftuiuitabbartabbarswiftui-navigationlink

how to switch tab programmatically on button click? in swiftui


I have implemented tab bar in my code. I have see all button in my first tab and from that button i want to switch to second tab programmatically. When I use navigationView then it creates another tab bar and moves to that screen and this changes the index of navigation in swiftui.

struct AppTabNavigation: View {
    @State var selection: Tab = .dashboard

    var body: some View {
        TabView(selection: $selection) {
            NavigationView {
                FirstTabView()
            }.navigationBarHidden(true)
            .navigationBarBackButtonHidden(true)
            .navigationViewStyle(StackNavigationViewStyle())
            
            .tabItem {
                Label("Home", systemImage: "house.fill")
                    .accessibility(label: Text("Home"))
            }
            .tag(Tab.home)
            NavigationView {
                SecondView()
            }.navigationBarHidden(true)
            .navigationBarBackButtonHidden(true)
            .navigationViewStyle(StackNavigationViewStyle())
            
            .tabItem {
                Label("Home", systemImage: "house.fill")
                    .accessibility(label: Text("Home"))
            }
            .tag(second) 
        }
     }
   }

Navigation Code:

NavigationLink(destination: AppTabNavigation(selection: Tab.home), isActive: self.$isActiveTabbar){
    Text("")
} .isDetailLink(false)

Solution

  • Here is a demo of possible approach - the idea is to move binding for tab selection into view with buttons, so button action could change it.

    Tested with Xcode 12 / iOS 14

    enum Tab {
        case dashboard
        case home
        case second
    }
    
    struct AppTabNavigation: View {
        @State var selection: Tab = .home
    
        var body: some View {
            TabView(selection: $selection) {
                NavigationView {
                    FirstTabView(tab: $selection)
                }.navigationBarHidden(true)
                .navigationBarBackButtonHidden(true)
                .navigationViewStyle(StackNavigationViewStyle())
                .tabItem {
                    Label("Home", systemImage: "house.fill")
                        .accessibility(label: Text("Home"))
                }
                .tag(Tab.home)
    
                NavigationView {
                    Text("SecondView")
                }.navigationBarHidden(true)
                .navigationBarBackButtonHidden(true)
                .navigationViewStyle(StackNavigationViewStyle())
    
                .tabItem {
                    Label("Home", systemImage: "house.fill")
                        .accessibility(label: Text("Home"))
                }
                .tag(Tab.second)
            }
        }
    }
    
    struct FirstTabView: View {
        @Binding var tab: Tab
        var body: some View {
            Button("Go Second") { self.tab = .second }
        }
    }