Search code examples
iosswiftipadswiftuimaster-detail

Force one NavigationLink to the detail view in SwiftUI


I have a master-(primary)-detail setup in my app, where most of the list items in the top-level NavigationView go to another list of submenu items, with .isDetailLink(false), and then those submenu items then navigate to the detail view. This is all working as intended.

I'm trying to also put in a NavigationLink in the top level list to got to my settings page, which I want to force into the detail view. There isn't a submenu for this link, so I tried to navigate to it directly and force .isDetailView(true). However, this causes the view to open in what would be the primary window above, but hidden at first. The detail window only has a back button, which then makes the intended view pop out from the menu column.

Is there a way to force only the settings menu item to open in the detail view, essentially skipping one navigation level?

Thanks!

Here's the simplified version of what I'm currently trying:

struct ContentView: View {
    var body: some View {

        NavigationView {
            List {
                NavigationLink(destination: SubMenu1()) {Text("MenuItem1")}
                    .isDetailLink(false)
                
                NavigationLink(destination: SubMenu2()) {Text("MenuItem2")}
                    .isDetailLink(false)
                
                NavigationLink(destination: SubMenu3()) {Text("MenuItem3")})
                    .isDetailLink(false)
                
                NavigationLink(destination: SettingsView()) {Text("Settings")}
                    .isDetailLink(true)
            }
        }

    }
}



struct SubMenu: View {
    var body: some View {
     
        List {
            ForEach(menuItems, id: \.self) { item in
                NavigationLink(destination: DetailView(item)) {
                    Text(item)
                }
            }
        }
    }
}

UPDATE I've taken some screenshots to illustrate the issue:

The top level navigation view: Top level Navigation View

With one of the sub-menus selected, and .isDetailView(false). This is working properly: With one of the sub-menus selected, and .isDetailView(false). This is working properly

Settings does not have a sub-menu like the others, so I want it to open directly on the right hand side. Instead, it opens like this, and is only revealed when the back button is pressed: Settings View enter image description here


Solution

  • Hey, I have seen your screenshots. I Think you are using NavigationView 2 times there in Settings. Please remove any one of them, It should work fine!