1

As there are some problems with iOS 13.4 and Xcode 11.4 with presentationMode.wrappedValue.dismiss() I am looking for an alternative approach to go back programmatically. I found this solution from MScottWaller:

iOS SwiftUI: pop or dismiss view programmatically

Unfortunately, in my case it does not work:

struct MasterView: View {
    @State private var showDetail = false

    var body: some View {
        VStack {
            Text("MasterView")
            .navigationBarItems(trailing: HStack {
                NavigationLink(destination: DetailView(showSelf: $showDetail), isActive: $showDetail) {
                    Image(systemName: "tag")
                        .padding(.leading, 4)
                }
            })
        }
    }
}

struct DetailView: View {
    @Binding var showSelf: Bool

    var body: some View {
        Button(action: {
            self.showSelf = false
        }) {
            Text("Pop")
        }
    }
}

If the NavigationLink is inside a navigationBarItem, I can't go back from my DetailView. I don't know if it is a bug or there are other reasons why NavigationLink does not work in the same way inside a navigationBarItem.

As a workaround I use this variant with a empty NavigationLink inside the view. It works, but I don't like this:

struct MasterView: View {
    @State private var showDetail = false

    var body: some View {
        VStack {
            Text("MasterView")
            NavigationLink(destination: DetailView(showSelf: $showDetail), isActive: $showDetail) {
                EmptyView()
            }
            .navigationBarItems(trailing: HStack {
                Button(action: { self.showDetail.toggle() }) {
                    Image(systemName: "tag")
                        .padding(.leading, 4)
                }
            })
        }
    }
}

Any ideas why the NavigationLink does not correct work inside a navigationBarItem?

Babelfish
  • 105
  • 1
  • 10
  • are you testing on real device ? – Jawad Ali Feb 06 '20 at 12:44
  • 1
    By my understanding of `NavigationView/NavigationLink` API description (and real behavior observations) the latter must be in navigation stack hierarchy, whereas `NavigationBar` is *above* navigation stack. – Asperi Feb 06 '20 at 13:00
  • Are you sure that using NavigationLink inside a navigationBarItems is an appropriate or supported use case? – user3441734 Feb 06 '20 at 13:23
  • @Asperi That is possible, but why does NavigationLink work in the NavigationBar at all? – Babelfish Feb 06 '20 at 13:58

1 Answers1

3

It's an iOS bug.

https://forums.developer.apple.com/thread/125937

The work around is to toggle a NavigationLink hidden outside of nav bar:

 struct Parent: View {  

    @State private var showingChildView = false  

    var body: some View {  
        NavigationView {  
            VStack {  
                Text("Hello World")  
                NavigationLink(destination: Child(),  
                               isActive: self.$showingChildView)  
                { Text("HiddenLink").hidden() }
            }  
            .navigationBarItems(trailing: Button(action:{ self.showingChildView = true }) { Text("Next") })  
        }  
    }  
}  
zavidovych
  • 4,718
  • 2
  • 24
  • 22