Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Navigation on iPad - How to show master list

Tags:

My app has simple navigation needs

  • List View (parent objects)
  • List View (child objects)
  • Detail View (child object)

I have this setup and working on iPhone, but when I run the app on iPad in portrait mode the master list is always hidden.

I'm using .isDetailLink(false) on the navigation link from the first list to the second, so both lists always stay in the master column. In iPad landscape everything works as expected but in portrait the detail view fills the screen. I can swipe in from the left side of the screen to show the list but I'd like to provide more clarity to the user.

I'd like to show or add the back button to show the master/list side (sort of like the Apple Notes app). On the iPhone I get the back button by default but on iPad in portrait mode there is nothing in its place.

This is what I see on iPhone enter image description here

But this is what I see on iPad enter image description here

Parent list

struct ParentList: View {      let firstList = ["Sample data 01", "Sample data 02", "Sample data 03", "Sample data 04", "Sample data 05"]      var body: some View {         NavigationView {             List{                 ForEach(firstList, id: \.self) { item in                     NavigationLink(destination: ChildList()){                          Text(item)                     }                     .isDetailLink(false)                  }             }         }     } } 

Child list

struct ChildList: View {      let secondList = ["More Sample data 01", "More Sample data 02", "More Sample data 03", "More Sample data 04", "More Sample data 05"]      var body: some View {         List{             ForEach(secondList, id: \.self) { item in                 NavigationLink(destination: ChildDetail()){                      Text(item)                  }             }         }     } } 

Child detail

struct ChildDetail: View {      var body: some View {         Text("Child detail view")     }  } 

Update: As of Oct 17, 2019 I have not found a way to get this to work. I decided to use .navigationViewStyle(StackNavigationViewStyle()) for the time being. Interestingly, this needs to go outside of the navigation view like a normal modifier, not inside it with the navigation title.

like image 611
radicalappdev Avatar asked Sep 11 '19 11:09

radicalappdev


People also ask

How do I manage navigation in SwiftUI?

SwiftUI's NavigationLink has a second initializer that has an isActive parameter, allowing us to read or write whether the navigation link is currently active. In practical terms, this means we can programmatically trigger the activation of a navigation link by setting whatever state it's watching to true.

How do I customize a list in SwiftUI?

Create the project To begin, create a SwiftUI Xcode project, and create a struct , namely, Data . Let's get back in our ContentView. swift and populate some values into this struct . Now, inside your view, create a List, and use ForEach to add and see all your data in list form.


1 Answers

In portrait the default split view does not work. This may be fixed in future but it appears the current options are:
(a) change the navigation view style of your first list to .navigationViewStyle(StackNavigationViewStyle()) so the navigation will work like on iPhone and push each view.

(b) leave the style to default and only support landscape for iPad

(c) implement a UIKit split view controller

like image 163
ptc Avatar answered Dec 22 '22 00:12

ptc