2

I have a TabView in SwiftUI with tabs. When I scroll list from one FirstView and tap another tab, and switch back to FirstView, my List in FirstView automatically redraws and scrolls to top. How to fix that.

enter link description here

This is FirstView

 var body: some View {
    NavigationView {
        List {
            ForEach(feed) { game in
                FeedCardItem(gameModel: game)
                    .frame(width: UIScreen.main.bounds.width - 30, height: 400)
            }
        }
        .navigationBarTitle(Text("Новое сегодня"))
    }
}

This is TabView implementation

    TabView (selection: $currentTab) {
        FeedView().tabItem {
            Image(systemName: currentTab == 0 ? "house.fill" : "house")
                .renderingMode(.original)
                .imageScale(.large)
        }.tag(0)

        RecommendationsView().tabItem {
            Image(systemName: currentTab == 1 ? "gamecontroller.fill" : "gamecontroller")
                .renderingMode(.original)
                .imageScale(.large)
        }.tag(1)

        SearchView().tabItem {
            Image(systemName: currentTab == 2 ? "flame.fill" : "flame")
                .renderingMode(.original)
                .imageScale(.large)
        }.tag(2)

        NotificationsView().tabItem {
            Image(systemName: currentTab == 3 ? "bell.fill" : "bell")
                .renderingMode(.original)
                .imageScale(.large)
        }.tag(3)

        ProfileView().tabItem {
            Image(systemName: currentTab == 4 ? "person.fill" : "person")
                .renderingMode(.original)
                .imageScale(.large)
        }.tag(4)

    }.edgesIgnoringSafeArea(.top)
}
annedroiid
  • 6,267
  • 11
  • 31
  • 54
Artyom Vlasenko
  • 385
  • 3
  • 16

3 Answers3

2

I have the same problem, and found this solution: https://kavsoft.dev/SwiftUI_2.0/Tab_Bar

Basically, you could just hide the content of one tab instead of replace it with another content view. I thought this solution is a little tricky but works.

Seth
  • 845
  • 8
  • 17
1

I don't think it is possible via SwiftUI. The alternate to do this is to create the tabview in UIKit and use it in SwiftUI.

I have created a quick sample of how this can be achieved and how it solves your issue. Let me know if you have any other questions.

I have uploaded the code to GitHub. https://github.com/subhan5/TabTest

In the uploaded code, the folder UITab contains the tabview created via UIKit. In ContentView, I convert the same as UIViewControllerRepresentable and use it.

Subha Narayanan
  • 442
  • 1
  • 4
  • 16
0

it is possible with the UITableViewConfigurator which is described here: Scroll SwiftUI List to new selection

But of course...this uses UIViewControllerRepresentable....

Chris
  • 7,579
  • 3
  • 18
  • 38