0

I have created demo of Tab bar with two tabItems. Each tab-item have view controller in it.

On switching tabs, it is not showing its respective UI.

This is happening on iOS 13 only.

Code:

struct ContentView: View {
    private enum Tab: Hashable {
           case one
           case two
    }
    @State private var selectedIndex = Tab.one
    var body: some View {
        TabView(selection: $selectedIndex) {
            DetestVXViewController(bgColor: .red).tabItem {
                Text("123")
            }.tag(Tab.one)
            DetestVXViewController(bgColor: .green).tabItem {
                Text("321")
            }.tag(Tab.two)
        }
       
    }
}

====

Attaching images of iOS 13 and 15 both.

enter image description here

enter image description here

Bhupesh Kumar
  • 369
  • 2
  • 18

1 Answers1

0

Using tag seems overkill unless you need to keep track of said tab for quick actions or another purpose. You can still use tag in this though if needed. You can use @State property for the selection also if needed. You can easily switch tabs with the following example.

enter image description here enter image description here

struct ContentView: View {
    var body: some View {
        TabView {
            ForEach(1..<3) { index in
                SomeView(title: "\(index)")
                    .tabItem {
                        Image(systemName: "\(index).circle.fill")
                        Text("\(index)")3
                    }
                //.tag(index)
            }
        }
    }
}
cole
  • 1,039
  • 1
  • 8
  • 35