Urkman
Urkman

Reputation: 1318

SwiftUI: List in a Tabview always reset after tab change

In my app I have a TabView with lists in there. When I scroll a list and than change tabs forth and back, the list is always reset and starts from the top...

But this shouldn't be the case... Is this a bug, or what I'm doing wrong?

Here is some example code:

struct ContentView: View {
   var body: some View {
       TabView {
            NavigationView {
                List {
                    ForEach((1...50), id: \.self) {
                        Text("Row in Tab 1 Number: \($0)")
                    }
                }
                .navigationBarTitle("Tab 1")
            }.tabItem {
                Image(systemName: "bubble.right")
                Text("Tab 1")
            }.tag(0)

            NavigationView {
                List {
                    ForEach((1...50), id: \.self) {
                        Text("Row in Tab 2 Number: \($0)")
                    }
                }
                .navigationBarTitle("Tab 2")
            }.tabItem {
                Image(systemName: "bubble.left")
                Text("Tab 2")
            }.tag(1)
        }.edgesIgnoringSafeArea(.top)
    }
}

Upvotes: 5

Views: 2106

Answers (2)

Urkman
Urkman

Reputation: 1318

OK..

This seems to be a known bug as mentioned here: SwiftUI Bugs on github

There you can find a workaround for this...

Upvotes: 1

Asperi
Asperi

Reputation: 257711

Taking into account that every view is a value, and on every update it is performed complete recreation of view hierarchy, and List for now does not have anything similar to visibleRange property or modifier... I would say it is a feature.

Upvotes: 1

Related Questions