SwiftUI NavigationView nests TabView auto pop
Asked Answered
C

2

3

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()
    }
}

Cistaceous answered 18/3, 2022 at 8:33 Comment(4)
Works fine with Xcode 13.3 / iOS 15.4 (ie. remains on navigated view after bring to foreground)Hoffarth
@Hoffarth I can't upgrade to 13.3 for the time being, is it compatible with versions below iOS 15.4?Cistaceous
I can confirm my iPhone XS ( iOS 15.4.1 ) suffers the same issueEmbosom
I faced the same problem. Did you manage to find the answer?Iphigenia
G
2

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.

Gitt answered 20/11, 2022 at 17:19 Comment(0)
M
0

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())
Martinmas answered 25/7 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.