I am trying to reproduce the behavior of a UISplitViewController
in SwiftUI, specifically on an iPad. This SwiftUI code does not behave as I'd expect:
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Master")
}.navigationBarItems(trailing: NavigationLink(destination: DetailView()) {
Image(systemName: "plus")
}).navigationBarTitle("Master List")
Text("")
}
}
}
struct DetailView: View {
var body: some View {
Text("Detail")
}
}
On an iPhone device, it works as one expects. Tapping the plus button in the navigation bar pushes a DetailView
on to the navigation stack and all seems fine.
On iPad, instead the plus pushes the detail view onto a navigation stack in the master view. It does this even if I explicitly tell it that the link should target the detail using isDetailLink(true)
.
Bug or am I going about this in the wrong way?
(This is leaving aside a secondary problem of how to avoid evaluating the destination View
until it is tapped. This is desirable when the destination view takes an argument (an empty model, for example) and creating that model has side effects (inserting an object into a managed object context, say). This blog explains the situation pretty well.)