MarcoGT
MarcoGT

Reputation: 53

SwiftUI: Strange behavior with Tabbar

My App has a TabBar with 3 bar items, as follows

var body: some View {
    TabView() {
        NavigationView {
            View1()
        }
        .tabItem { Label("Tab1"), systemImage: "calendar.badge.exclamationmark") }
        
        View2()
            .tabItem {
                Label("Tab2"), systemImage: "list.bullet")
            }
        
        View3()
            .tabItem {
                Label("Tab3"), systemImage: "info")
            }
    }
}

What I do not understand is why Tab2 appears 2 times, it means I have in total 4 tabs instead of 3. I am feeling quite stupid :-)

Upvotes: 1

Views: 197

Answers (1)

aheze
aheze

Reputation: 30318

You had a couple misplaced parentheses () that might have been the problem. Try this code (replace Text("View one") with View1() and so on):

struct ContentView: View {
    var body: some View {
        TabView {
            NavigationView {
                Text("View one")
            }
            .tabItem {
                Label("Tab1", systemImage: "calendar.badge.exclamationmark")

            }

            Text("View two")
                .tabItem {
                    Label("Tab2", systemImage: "list.bullet")

                }

            Text("View three")
                .tabItem {
                    Label("Tab3", systemImage: "info")

                }
        }
    }
}

Result:

3 tabs

Upvotes: 1

Related Questions