`PageTabViewStyle` for `TabView` doesn't work with adding new Page

I try to use a PageTabViewStyle for TabView but it seems to be broken. It works fine when I use a static preloaded content.

Code Block swift
TabView {
ForEach(viewModel.objects) { object in
ObjectView(object)
    }
}
.tabViewStyle(PageTabViewStyle())               
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))

But when I try to add a new Page there

Code Block swift
viewModel.objects.append(newObject)

It affects only the IndexIndicator (add one more dot there), but not the content. A new object doesn't appear and I can't get scroll to it.

viewModel is a @StateObject and objects in viewModel is a @Published property

Answered by Fat Xu in 622267022

Code Block swift
struct TabViewTest: View {
    @State private var selection = 0
    @State private var contents = ["a","b","c"]
    @State private var update = UUID()
    var body: some View {
        VStack{
        TabView(selection:$selection){
            ForEach(0..<contents.count,id:\.self){ i in
                Text(contents[i])
                    .tag(i)
            }
        }
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
        .id(update)
            Button("add"){
                contents.append("ddd")
                let old = selection
                update = UUID()
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
                    selection = old
                }
            }
        }
    }
}


Accepted Answer

Code Block swift
struct TabViewTest: View {
    @State private var selection = 0
    @State private var contents = ["a","b","c"]
    @State private var update = UUID()
    var body: some View {
        VStack{
        TabView(selection:$selection){
            ForEach(0..<contents.count,id:\.self){ i in
                Text(contents[i])
                    .tag(i)
            }
        }
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
        .id(update)
            Button("add"){
                contents.append("ddd")
                let old = selection
                update = UUID()
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
                    selection = old
                }
            }
        }
    }
}


`PageTabViewStyle` for `TabView` doesn't work with adding new Page
 
 
Q