Farhandika
Farhandika

Reputation: 507

TabView Selection does not work in SwiftUi Playground

So i tried to add a simple TabView in Swiftui, i test it with a ForEach loop and it works like a charm but when i didnt use a ForEach loop, it does not work

So what did i do wrong

VStack{
        TabView(selection: $Selection){
            Text("Test1")
            Text("Test2")
            Text("test3")
        }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
        .foregroundColor(.black)
        Group{
            HStack{
                ForEach(0..<3){i in
                    Button(action: {
                        withAnimation {
                            Selection = i
                        }
                    }) {
                        Circle()
                        .frame(width:100, height:70)
                        .foregroundColor (.red)
                }
            }
        }
        }
}

Ps : currently using Swift Playground for MAC

Upvotes: 0

Views: 151

Answers (1)

davidev
davidev

Reputation: 8517

Add the .tag() to your Items inside the TabView like this...:

TabView(selection: $Selection){
    Text("Test1").tag(0)
    Text("Test2").tag(1)
    Text("test3").tag(2)
}.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))

Upvotes: 1

Related Questions