3

I have a split view in my iPad ready app:

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink("Show the slave view HERE", destination: SlaveView())
                    .navigationBarTitle("Master view")
            }

            Text("Detail view")
                .navigationBarTitle("DO NOT show the slave view here")
        }
    }
}

So I like the SlaveView view to open in the list itself, not in the detail view. I have tried setting another NavigationView in the Slave, also a text below that, also setting all navigationViewStyles on both and each Master and Slave with no luck.

This is the simplest Slave view you can have to make it build:

struct SlaveView: View {
    var body: some View {
        List {
            NavigationLink("Sub Detail view", destination: Text("Sub Detail view"))
        }
        .navigationBarTitle("Slave view")
    }
}

So how can I change the Master (left) view of the split view instead of the detail (right) view?

Note that this is a simplified reproducible code. The real project uses more complex lists for master and slaves and so on. Also, we don't want to loos navigation stuff like transitions, title transforming, back button and etc.

For more clarifying, I need this state in the flow:

Needed step

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278

3 Answers3

4

Just modify link that it is not a detail

demo

NavigationLink("Show the slave view HERE", destination: SlaveView())
    .isDetailLink(false)
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • +1 for point to the right modifier. But it has a strange bug! Let me show you with a gif: https://i.stack.imgur.com/c09sb.gif. Just one click does this ‍♂️ – Mojtaba Hosseini Jul 30 '20 at 07:43
  • It looks like you activate many links at once. Look at [this topic](https://stackoverflow.com/questions/63012568/swifui-activates-all-available-indices-in-foreach) - visually you have something similar. – Asperi Jul 30 '20 at 07:47
  • Looks like `.listStyle(GroupedListStyle())` cuse this issue. – Mojtaba Hosseini Jul 30 '20 at 07:52
0

You may try using a Button instead of a NavigationLink and replace your master view:

struct ContentView: View {
    @State var showSlaveView = false

    var body: some View {
        NavigationView {
            masterView
                .navigationBarTitle("Master view")
            Text("Detail view")
                .navigationBarTitle("DO NOT show the slave view here")
        }
    }
}

extension ContentView {
    @ViewBuilder
    var masterView: some View {
        if showSlaveView {
            SlaveView()
                .onTapGesture { self.showSlaveView = false }
        } else {
            Button("Show the slave view HERE") {
                self.showSlaveView = true
            }
        }
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
  • +1 for the idea. But it kills all navigation transition, title transition, back button, swipe gesture, and so on. Also, in the complex app. Master and slaves are complex lists. What about that? – Mojtaba Hosseini Jul 29 '20 at 22:43
  • @MojtabaHosseini But this would mean two NavigationLinks open at the same time. From the master you open the slave, but the detail is already opened (also from the master). I don't think that SwiftUI navigation stack allows that currently. – pawello2222 Jul 29 '20 at 22:52
  • I need something like: keep lists on the left and details on the right. But I think like you too, unfortunately. – Mojtaba Hosseini Jul 29 '20 at 22:54
-1

You can try using StackNavigationViewStyle()

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink("Show the slave view HERE", destination: SlaveView())
                .navigationBarTitle("Master view")
            
            Text("Detail view")
                .navigationBarTitle("DO NOT show the slave view here")
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}