7

I'm using a NavigationView and NavigationLinks to move from a MasterView to a Detail1View and then further to a Detail2View.

On an iPhone in portrait mode, the back button is displayed for all detail views and allows to move back from Detail2View to Detail1View, and then further to MasterView.

But on an iPhone in landscape mode or on an iPad, when moving from Detail1View to Detail2View, there is no back button. And it is thus impossible to go back to Detail1View.

Adding a 2nd NavigationView allows to have a back button, but it's not really a desirable workaround, as the 2nd UINavigationViewController is shown below the 1st one.

struct MyMasterView: View {
    private let names = ["Homer", "Marge", "Bart", "Lisa"]

    var body: some View {
        List() {
            Section(header: Text("The Simpsons")) {
                ForEach(names, id: \.self) { name in
                    NavigationLink(destination: MyDetail1View(name: name)) {
                        Text(name)
                    }
                }
            }
        }
        .navigationBarTitle(Text("My App"))
    }
}

struct MyDetail1View: View {
    var name: String

    var body: some View {
        //NavigationView {
            List {
                NavigationLink(destination: MyDetail2View(name: name)) {
                    Text("Hello \(name)")
                }
            }
            .navigationBarTitle(Text("Details 1"))
        //}
    }
}

struct MyDetail2View: View {
    var name: String

    var body: some View {
        HStack {
            Text("\(name), I'm sorry but in Landscape there is no back button...")
        }
        .navigationBarTitle(Text("Details 2"))
    }
}

struct ContentView : View {
    var body: some View {
        NavigationView {
            MyMasterView()
            Text("In Landscape, swipe to start...")
        }
    }
}
caram
  • 1,494
  • 13
  • 21
  • I'm seeing the same behaviour (Xcode 11 beta 5) and I'm tempted to say that it is a bug so I filled a report with your snippets – Tae Aug 15 '19 at 14:34
  • 1
    It's my opinion as well. I have filed a bug report also. Hopefully this will increase the likelihood of it being fixed. – caram Aug 17 '19 at 11:55

1 Answers1

9

The answer turns out to be as simple as using isDetailLink()!

NavigationLink(destination: MyDetail2View(name: name)) {
   Text("Hello \(name)")
}.isDetailLink(false)

Credits to https://stackoverflow.com/a/57400873/2893408

caram
  • 1,494
  • 13
  • 21
  • 1
    Well it seemed to work but it's buggy on iPad in iOS 13.1. The detail view2 will disappear at random as soon as you start to interact with the view. Anyone found a fix? – caram Oct 13 '19 at 10:01