SwiftUI TabbedView only shows first tab's content SwiftUI TabbedView only shows first tab's content swift swift

SwiftUI TabbedView only shows first tab's content


Try adding tags:

    TabbedView {        Text("Hello world")            .tabItem { Text("Hello") }            .tag(0)        Text("Foo bar")            .tabItem { Text("Foo") }            .tag(1)    }


I was able to fix this by adding a selection state variable and passing that in for the selection:

struct ContentView : View {    @State private var selection = 1    var body: some View {        TabbedView(selection: $selection) {            Text("Tab 1!").tabItemLabel(                Text("Tab 1")).tag(1)            Text("Tab 2!").tabItemLabel(Text("Tab 2")).tag(2)        }    }}

Now, tapping "Tab 2" will show "Tab 2!" on the screen, as opposed to a blank screen.

This was using Xcode 11.0 beta 2 (11M337n), macOS Catalina 10.15 Beta (19A487l).


In the newest version you should use TabView:

   TabView {        AnyView()            .tabItem {                Text("Label 1")            }        AnyView()            .tabItem {                Text("Label 2")            }    }