2

i want to use TabView to display some data. I'm using paged view style for this.

struct ContentView: View {
  @State var texts: [String] = ["first", "second"]

  var body: some View {
    TabView {

        ForEach(texts, id: \.self) { text in
            VStack {
                
                Button(action: {
                    texts.append("next one...")
                }, label: {
                    Text("Button")
                })
                
                Text(text)
            }
        }
    }
    .tabViewStyle(PageTabViewStyle())
    .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
  }
}

As expected the view comes up to display two pages ("first" and "second"). When i'm pressing the button a new page indicator is added (so i see three dots in the page index) but i can't scroll to the third page.

Am i missing something, or is this a bug in Xcode?

Thieri
  • 203
  • 1
  • 10

1 Answers1

0

So as Asperi already pointed out the same beheaviour was seen in TabView with "PageTabViewStyle" does not update it's content when @State var changes

I also found this post which references the same answer: https://www.hackingwithswift.com/forums/swiftui/how-do-i-append-a-new-page-into-tabview-pagetabviewstyle/2583

The solution would be to ad an .id( id value ) to the TabView, so that it reloads completely.

Thieri
  • 203
  • 1
  • 10