NavigationView SwiftUI shows split view in iPad

Viewed 1277

With NavigationView being the root of UIHostingController , the below code shows split view for iPad.

struct ContentView: View {
    var body: some View {
        
        NavigationView {
            Text("Hello")
                .navigationBarTitle("Home")
        }
    }
}

With the above code it shows split view on iPad. How can I still use the NavigationView and get rid of split view for iPad , as I am looking to have a List and on tap of which it should push another view?

enter image description here enter image description here

2 Answers

Use stack navigation view style explicitly (by default it is platform-dependent)

NavigationView {
   Text("Hello")
       .navigationBarTitle("Home")
}
.navigationViewStyle(StackNavigationViewStyle())

That did not work for me nor did using any other NavigationStyle on iPad using IOS 14.2. The root view always looks like this.

    var body: some View {
    NavigationView {
        VStack {
            List {
                ForEach(self.ideas) { Idea in
                    IdeaListRow(idea: Idea)
                }
                .onDelete { (indexSet) in
                    let ideaToDelete = self.ideas[indexSet.first!]
                    self.managedObjectContext.delete(ideaToDelete)
                    
                    do {
                        try self.managedObjectContext.save()
                    } catch {
                        print(error)
                    }
                }
            }
            .navigationViewStyle(DoubleColumnNavigationViewStyle())
            .navigationBarTitle(Text("Idea List"))
            .listStyle(GroupedListStyle())
            .navigationBarItems(leading:
                                    NavigationLink(destination: AddView()) {
                                        Text("Add")
                                    } , trailing: EditButton())
        }
    }
}

enter image description here

Related