Hi Ziqiao,
thanks for the code snippet — it also works well in my app. I only made a few small changes:
I’m using NavigationSplitView instead of NavigationStack, and placing the List inside the navigation view slightly changes the appearance of the navigation bar. It now returns directly from the detail view to the document browser.
Also, the sheets and full-screen covers you present should now show the document title and back buttons in their navigation bars.
Xcode 16.4
iOS 18.3
iPhone 18 Pro (Simulator)
struct ContentView: View {
private enum Row: Hashable { case details }
@State private var selection: Row?
var body: some View {
TabView {
Tab("Rectangle", systemImage: "rectangle") {
NavigationSplitView {
List(selection: $selection) {
NavigationLink(value: Row.details) {
Text("Tap to see details")
}
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button("Add") {
print("Button pressed.")
}
}
}
} detail: {
switch selection {
case .details:
Text("Details")
default:
Text("Select something")
}
}
}
Tab("Circle", systemImage: "circle") {
NavigationStack {
Text("Circle")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button("Remove") {
print("Button pressed.")
}
}
}
}
}
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: