7

I'm getting a strange crash from pretty normal navigation on my SwiftUI app

I have a simple tab view :

struct FFTabView: View {
    var body: some View {
        TabView {
            LibraryView2()
        }
        .navigationBarBackButtonHidden(true)
        .navigationBarHidden(true)
        .navigationBarTitle("", displayMode: .inline)
    }
}

// MARK: -

struct LibraryView2: View {

    var body: some View {
        VStack {
            NavigationLink(destination: Foo()) {
                Text("go to foo")
            }
        }
        .tabItem {
            Image(systemName: "square.grid.2x2.fill")
            Text("Skill Library")
        }
    }

}

struct Foo: View {
    var body: some View {
        Text("foo view")
    }
}

When I go back via my nav bar, from Foo I get a crash: Tried to pop to a view controller that doesn't exist

Any idea what's going on here? I can't find anything related to this and SwiftUI so figured I'd post. Thanks

Zack Shapiro
  • 6,648
  • 17
  • 83
  • 151

1 Answers1

5

Although you didn't specify, I assume your FFTabView is wrapped in a NavigationView somewhere.

Ultimately, then, your view hierarchy looks like

NavigationView {
    TabView {
        NavigationLink {
            ...
        }
    }
}

If you restructure your view hierarchy so it's like

TabView {
    NavigationView {
        NavigationLink {
            ...
        }
    }
}

The crash doesn't happen.

Edit: I have confirmed that it is related to the regression/bug discussed in this answer, introduced in Xcode 11.2. Your original code works fine in Xcode 11.1.

John M.
  • 8,892
  • 4
  • 31
  • 42
  • Ah! Thanks John. This is just what I was looking for. Since I'm prototyping, I removed the login view as no one needs to log in yet which allowed me to wrap the innards of the `LibraryView2` in a `NavigationView` which solved the crasher – Zack Shapiro Nov 06 '19 at 23:32
  • The problem with this solution is that some app layouts require the top view hierarchy. I have such an app myself, and switching to the lower hierarchy breaks the animation of the child navigation views by not hiding the tab bar. Anybody have any ideas of how to fix this crash bug WITHOUT having to fundamentally change the app structure? – eResourcesInc Jan 28 '20 at 15:59
  • @eResourcesInc I have the same problem as you. Did you find a solution? Would be great im you can tag me If you answer – Lukas Dec 10 '20 at 18:06