NavigationLink inside of a navigationBarItems

Why does my swiftUI app crash when navigating backwards after placing a NavigationLink insiede of a NavigationBarItems?


NavigationView {
  Text("Hello World")
  .navigationBarItems(
     trailing: NavigationLink(destination: Child()) {
          Image("myImageName"})
  }

Accepted Reply

This appears as a workaround, waiting for the bug to be corrected (you may have to read the whole post as it references some other code (Robert's Parent View):


It looks like the issue revolves around the Nav Bar and the way the buttons were added to it. So instead of using a NavigationLink() for the button itself, I tried using a standard Button() with an action that sets a @State var that activates a hidden NavigationLink. Here is a replacement for Robert's Parent View:



struct Parent: View {
    @State private var showingChildView = false
    var body: some View {
        NavigationView {
            VStack {
                Text("Hello World")
                NavigationLink(destination: Child(),
                               isActive: self.$showingChildView)
                { EmptyView() }
                    .frame(width: 0, height: 0)
                    .disabled(true)            }
                .navigationBarItems(
                    trailing: Button(action:{ self.showingChildView = true }) { Text("Next") }
                )
        }
    }
}


So you should:

- comment out your present code and add comment as "Waiting for iOS 13.2 bug correction" (to be able to restore when bug is finally corrected)

- replace by something similar to this one

- test and report

Replies

Apparently there are issues on iOS 13.2. Is it your version of iOS ?


See here for analysis and some advices to workaround.


https://stackoverflow.com/questions/58404725/why-does-my-swiftui-app-crash-when-navigating-backwards-after-placing-a-navigat


But maybe that is your own post one month ago ?

Yes I have ios 13.2, there is a way to do the same thing?

No, I started using swiftUI one week ago.

This appears as a workaround, waiting for the bug to be corrected (you may have to read the whole post as it references some other code (Robert's Parent View):


It looks like the issue revolves around the Nav Bar and the way the buttons were added to it. So instead of using a NavigationLink() for the button itself, I tried using a standard Button() with an action that sets a @State var that activates a hidden NavigationLink. Here is a replacement for Robert's Parent View:



struct Parent: View {
    @State private var showingChildView = false
    var body: some View {
        NavigationView {
            VStack {
                Text("Hello World")
                NavigationLink(destination: Child(),
                               isActive: self.$showingChildView)
                { EmptyView() }
                    .frame(width: 0, height: 0)
                    .disabled(true)            }
                .navigationBarItems(
                    trailing: Button(action:{ self.showingChildView = true }) { Text("Next") }
                )
        }
    }
}


So you should:

- comment out your present code and add comment as "Waiting for iOS 13.2 bug correction" (to be able to restore when bug is finally corrected)

- replace by something similar to this one

- test and report