Post

Replies

Boosts

Views

Activity

How do you dismiss the detail view in SwiftUI on an iPad/split view layout?
I'm attempting to dismiss the detail view in a master detail view on an iPad (e.g. if that item is deleted in the master view and needs to blank out the right hand side). import SwiftUI struct ContentView: View { @State var selection: String? var body: some View { NavigationView { List { NavigationLink(tag: "item1", selection: $selection) { DetailView(item: "Item 1", selection: $selection) } label: { Text("Item 1") } NavigationLink(tag: "item2", selection: $selection) { DetailView(item: "Item 2", selection: $selection) } label: { Text("Item 2") } NavigationLink(tag: "item3", selection: $selection) { DetailView(item: "Item 3", selection: $selection) } label: { Text("Item 3") } } Text("Blank View") } } } struct DetailView: View { let item: String @Binding var selection: String? @Environment(\.dismiss) var dismiss var body: some View { VStack { Text("Detail for \(item)") Button { selection = nil // dismiss() } label: { Text("Dismiss") } } } } This is an example that demonstrates the issue. When dismiss is pressed it nils the selection which on iPhone dismisses the detail controller. On the iPad it keeps the detail view there, rather than returning to the blank view. Here is the view before selecting an item. You can see the blank view on the right hand side. After selecting an item the left hand side is selected, and the right hand side shows the appropriate detail view. After dismissing the view by nilling the selection (or using the environment dismiss) the selection on the left disappears as it should, but the detail view on the right stays put. This should have disappeared and the blank view should show again.
1
0
593
Dec ’21