Skip to content

Instantly share code, notes, and snippets.

@VAndrJ
Last active November 14, 2024 14:06
Show Gist options
  • Save VAndrJ/19fae6cad7d012957636ea0d9ddcf260 to your computer and use it in GitHub Desktop.
Save VAndrJ/19fae6cad7d012957636ea0d9ddcf260 to your computer and use it in GitHub Desktop.
NavigationStack path clear issue on iOS 18.0+
import SwiftUI
@main
struct TestNavigationSearchApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State private var path: [String] = []
var body: some View {
NavigationStack(path: $path) {
ExampleView(path: $path, title: "Root")
.navigationDestination(for: String.self) { title in
ExampleView(path: $path, title: title)
}
}
}
}
struct ExampleView: View {
@Binding var path: [String]
let title: String
@State private var text = ""
@State private var isSearchPresented = true
var body: some View {
Text(#"Enter the title and press "search""#)
.navigationBarTitle(title)
.toolbar {
if !path.isEmpty {
ToolbarItem {
Button("Root") {
path.removeAll()
}
}
}
}
.searchable(text: $text, isPresented: $isSearchPresented)
.onSubmit(of: .search) {
path.append(text)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment