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
You can display a list for example")
}
}
.navigationBarTitle("Master")
// The second View is the Detail
Text("Detail placeholder
Here 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).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…