Why does my SwiftUI app crash when navigating backwards after placing a `NavigationLink` inside of a `navigationBarItems` in a `NavigationView`?

后端 未结 9 1133
轮回少年
轮回少年 2020-12-04 12:25

EDIT: This has been fixed in iOS 13.3!

Minimal reproducible example (Xcode 11.2 beta, this works in Xcode 11.1):

struct Parent: View {         


        
相关标签:
9条回答
  • 2020-12-04 12:47

    FWIW - The solutions above suggesting a hidden NavigationLink Hack is still the best workaround in iOS 13.3b3. I have also filed a FB7386339 for posterity's sake, and was closed similarly to other aforementioned FBs: "Potential fix identified - For a future OS update".

    Fingers Crossed.

    0 讨论(0)
  • 2020-12-04 12:56

    As a workaround, based on Chuck H's answer above, I've encapsulated the NavigationLink as a hidden element:

    struct HiddenNavigationLink<Content: View>: View {
    var destination: Content
    @Binding var activateLink: Bool
    
    var body: some View {
        NavigationLink(destination: destination, isActive: self.$activateLink) {
            EmptyView()
        }
        .frame(width: 0, height: 0)
        .disabled(true)
        .hidden()
    }
    }
    

    Then you can use it within a NavigationView (which is crucial) and trigger it from a Button in a nav bar:

    VStack {
        HiddenNavigationList(destination: SearchView(), activateLink: self.$searchActivated)
        ...
    }
    .navigationBarItems(trailing: 
        Button("Search") { self.searchActivated = true }
    )
    

    Wrap this in "//HACK" comments so when Apple fixes this you can replace it.

    0 讨论(0)
  • 2020-12-04 12:56

    Based on the information that you guys provided and specially a comment that @Robert made about where the NavigationView is placed I have found a way to workaround the issue at least on my specific scenario.

    In my case I had a TabView that was enclosed in a NavigationView like this:

    struct ContentViewThatCrashes: View {
    @State private var selection = 0
    
    var body: some View {
        NavigationView{
            TabView(selection: $selection){
                NavigationLink(destination: NewView()){
                    Text("First View")
                        .font(.title)
                }
                .tabItem {
                    VStack {
                        Image("first")
                        Text("First")
                    }
                }
                .tag(0)
                NavigationLink(destination: NewView()){
                    Text("Second View")
                        .font(.title)
                }
                .tabItem {
                    VStack {
                        Image("second")
                        Text("Second")
                    }
                }
                .tag(1)
            }
        }
      }
    }
    

    This code crashes as everyone is reporting in iOS 13.2 and works in iOS 13.1. After some research I figured out a workaround to this situation.

    Basically, I am moving the NavigationView to each screen separately on each tab like this:

    struct ContentViewThatWorks: View {
    @State private var selection = 0
    
    var body: some View {
        TabView(selection: $selection){
            NavigationView{
                NavigationLink(destination: NewView()){
                    Text("First View")
                        .font(.title)
                }
            }
            .tabItem {
                VStack {
                    Image("first")
                    Text("First")
                }
            }
            .tag(0)
            NavigationView{
                NavigationLink(destination: NewView()){
                    Text("Second View")
                        .font(.title)
                }
            }
            .tabItem {
                VStack {
                    Image("second")
                    Text("Second")
                }
            }
            .tag(1)
        }
      }
    }
    

    Somehow goes against the SwiftUI premise of simplicity but it works on iOS 13.2.

    0 讨论(0)
提交回复
热议问题