11

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

NNikN
  • 3,720
  • 6
  • 44
  • 86

2 Answers2

22

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

NavigationView {
   Text("Hello")
       .navigationBarTitle("Home")
}
.navigationViewStyle(StackNavigationViewStyle())
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • 1
    Works great! Just a note for others: the `.navigationViewStyle` modifier must be attached directly to the `NavigationView`, as shown here. I originally had it attached to my inner view and it did not work. – coping Nov 29 '20 at 18:04
  • Took me a while to find this. Beautiful. – zumzum Nov 16 '21 at 17:18
1

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

tgunr
  • 1,540
  • 17
  • 31