iosswiftuitabbed-view

Views content not showing on switching to other tabs using TabbedView of SwiftUI


I'm implementing TabbedView using SwiftUI framework by referring https://developer.apple.com/documentation/swiftui/tabview

When running on the simulator, only the first tab view contents showing and other tabs contents not showing. Even after restarting XCode, simulator etc.

App video link: https://youtu.be/Gibu8jfQQ5I

struct ContentView : View {
    var body: some View {
         TabbedView {
            Text("The First Tab")
                .tabItem {
                    Image(systemName: "1.square.fill")
                    Text("First")
            }
            Text("Another Tab")
                .tabItem {
                    Image(systemName: "2.square.fill")
                    Text("Second")
            }
            Text("The Last Tab")
                .tabItem {
                    Image(systemName: "3.square.fill")
                    Text("Third")
            }
        }.font(.headline)
    }
}

Appreciate you help and suggestions!


Solution

  • In beta 5, your code works, and also TabbedView was renamed to TabView. If you cannot upgrade to beta 5 yet, to fix your problem in beta 4, you need to add .tag(n) to each view:

    struct ContentView : View {
        var body: some View {
             TabbedView {
                Text("The First Tab").tag(1)
                    .tabItem {
                        Image(systemName: "1.square.fill")
                        Text("First")
                }
                Text("Another Tab").tag(2)
                    .tabItem {
                        Image(systemName: "2.square.fill")
                        Text("Second")
                }
                Text("The Last Tab").tag(3)
                    .tabItem {
                        Image(systemName: "3.square.fill")
                        Text("Third")
                }
            }.font(.headline)
        }
    }