SwiftUI Navigation on iPad - How to show master list

后端 未结 3 987
误落风尘
误落风尘 2020-12-31 01:31

My app has simple navigation needs

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

I have this

相关标签:
3条回答
  • 2020-12-31 02:04

    in iOS 13.4, a "back to master view" button has been added to the iPad layout. From the release notes:

    When using a NavigationView with multiple columns, the navigation bar now shows a control to toggle the columns. (49074511)

    For example:

    struct MyNavView: View {
        var body: some View {
            NavigationView {
                NavigationLink(
                    destination: Text("navigated"), 
                    label: {Text("Go somwhere")}
                )
                .navigationBarTitle("Pick Item")
            }
        }
    }
    

    Has the following result:

    0 讨论(0)
  • 2020-12-31 02:06

    There also is a quite hacky workaround (see https://stackoverflow.com/a/57215664/3187762)

    By adding .padding() to the NavigationView it seems to achieve the behaviour of always display the Master.

    NavigationView {
            MyMasterView()
            DetailsView()
     }.navigationViewStyle(DoubleColumnNavigationViewStyle())
      .padding()
    

    Not sure if it is intended though. Might break in the future (works using Xcode 11.0, in simulator on iOS 13.0 and device with 13.1.2).

    You should not rely on it. This comment seems to be the better answer: https://stackoverflow.com/a/57919024/3187762

    0 讨论(0)
  • 2020-12-31 02:17

    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

    0 讨论(0)
提交回复
热议问题