Learn Courses My Dashboard

Problem placing Navigation View in a tab view

When I place a navigation view or navigation link in a tab view the tab item disappears. I can’t figure out why.

import SwiftUI

struct ContentView: View {
    
    @State var tabIndex = 0
    
    var body: some View {
        TabView(selection: $tabIndex) {
            
            // MARK: Tab 1
            Text("This tab's tag is \(tabIndex).")
                .tabItem {
                    VStack {
                        Image(systemName: "tortoise")
                        Text("Tab 1")
                    }
                }
                .tag(0)
            
            // MARK: Tab 2
            Button("Take me to tab 3!") {
                tabIndex = 2
            }
            .tabItem {
                VStack {
                    Image(systemName: "arrow.right.square")
                    Text("Tab 2")
                }
            }
            .tag(1)
            
            // MARK: Tab 3
            NavigationView{
            List {
                ForEach(0..<100) { _ in
                    Text("This is tab 3!")
                }
            }
            .tabItem {
                VStack {
                    Image(systemName: "hands.clap")
                    Text("Tab 3")
                }
            }
            }
            .tag(2)
            
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Attach your tabItem to the NavigationView, not the List inside the NavigationView.

Thanks