What's the equality of the UISplitViewController in SwiftUI

前端 未结 1 733
广开言路
广开言路 2020-12-21 18:45

I need to implement an UI which close to the default Mail app in iPad and iPhone.

The App has two sections, typically, the master view will be displayed on the left

相关标签:
1条回答
  • 2020-12-21 19:15

    There is not really a SplitView in SwiftUI, but what you describe is automatically accomplished when you use the following code:

    import SwiftUI
    
    struct MyView: View {
        var body: some View {
            NavigationView {
                // The first View inside the NavigationView is the Master
                List(1 ... 5, id: \.self) { x in
                    NavigationLink(destination: SecondView(detail: x)) {
                        Text("Master\nYou can display a list for example")
                    }
                }
                .navigationBarTitle("Master")
                // The second View is the Detail
                Text("Detail placeholder\nHere you could display something when nothing from the list is selected")
                    .navigationBarTitle("Detail")
            }
        }
    }
    
    struct SecondView: View {
        var detail: Int
        var body: some View {
            Text("Now you are seeing the real detail View! This is detail \(detail)")
        }
    }
    

    This is also why the .navigationBarTitle() modifier should be applied on the view inside the NavigationView, instead of on the NavigationView itself.

    Bonus: if you don't like the splitView navigationViewStyle, you can apply the .navigationViewStyle(StackNavigationViewStyle()) modifier on the NavigationView.

    Edit: I discovered that the NavigationLink has an isDetailLink(Bool) modifier. The default value appears to be true, because by default the "destination" is presented in the detail view (on the right). But when you set this modifier to false, the destination is presented as a master (on the left).

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