gaohomway
gaohomway

Reputation: 4080

SwiftUI NavigationView nests TabView auto pop

NavigationView nests TabView, I have a List, and push to the next page When the application returns to the background and returns to the active state, the push page automatically pops up.

If TabView nests NavigationView, there will be no problem, but I want NavigationView to nest TabView, is there any way to solve it


struct ContentView: View {
    
    var body: some View {
        
        NavigationView {
            TabView {
                List {
                    ForEach(0..<30) { index in
                        RowView(index: index)
                    }
                }
            }
        }
    }
}



struct RowView: View {
    
    var index: Int
    @State var userViewActive: Int?
    
    var body: some View {
        NavigationLink {
            Text("Hello, world!")
        } label: {
            Text("Hello, world!")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 3

Views: 608

Answers (2)

Yunus Gedik
Yunus Gedik

Reputation: 190

This bug does not occur on ios 15 but occurs on later versions. After spending some "time" in this lovely bug, I end up using a custom Tab solution. It is easy to use and customizable, check it out:

https://github.com/onl1ner/TabBar/tree/master

    TabBar(selection: $selection, visibility: $visibility) {
        Text("First")
            .tabItem(for: Item.first)
        
        Text("Second")
            .tabItem(for: Item.second)
        
        Text("Third")
            .tabItem(for: Item.third)
    }
    .tabBar(style: CustomTabBarStyle())
    .tabItem(style: CustomTabItemStyle())

Upvotes: 0

Bas9990
Bas9990

Reputation: 146

In a tabview each tab should manage its own view state. Aka

Every tab should have it's own navigation view. If you add this, you will see that your problem is solved.

Upvotes: 2

Related Questions