{"id":25457926,"url":"https://github.com/cuadros-code/project-9-swiftui-navigation","last_synced_at":"2025-06-30T03:36:26.016Z","repository":{"id":263915232,"uuid":"891156231","full_name":"cuadros-code/Project-9-SwiftUI-navigation","owner":"cuadros-code","description":"we’ll be focusing on navigation","archived":false,"fork":false,"pushed_at":"2024-12-04T21:27:31.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-16T20:09:02.725Z","etag":null,"topics":["hashable","navigationdestination","navigationlink","navigationpath","navigationstack"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cuadros-code.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-11-19T20:35:08.000Z","updated_at":"2024-12-04T21:27:34.000Z","dependencies_parsed_at":"2025-05-16T20:09:03.488Z","dependency_job_id":"82a902e3-a0fe-4f98-a2f9-e411e518e48e","html_url":"https://github.com/cuadros-code/Project-9-SwiftUI-navigation","commit_stats":null,"previous_names":["cuadros-code/project-9-swiftui-navigation"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cuadros-code/Project-9-SwiftUI-navigation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuadros-code%2FProject-9-SwiftUI-navigation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuadros-code%2FProject-9-SwiftUI-navigation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuadros-code%2FProject-9-SwiftUI-navigation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuadros-code%2FProject-9-SwiftUI-navigation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cuadros-code","download_url":"https://codeload.github.com/cuadros-code/Project-9-SwiftUI-navigation/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuadros-code%2FProject-9-SwiftUI-navigation/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262705166,"owners_count":23351199,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hashable","navigationdestination","navigationlink","navigationpath","navigationstack"],"created_at":"2025-02-18T02:19:32.288Z","updated_at":"2025-06-30T03:36:25.985Z","avatar_url":"https://github.com/cuadros-code.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Navigation \n\n## Handling navigation the smart way with navigationDestination()\n\n```swift\nstruct Student: Hashable {\n    var id = UUID()\n    var name: String\n    var age: Int\n}\n\nstruct ContentView: View {\n    \n    var students: [Student] = [\n        Student(name: \"Kevin\", age: 25),\n        Student(name: \"Julio\", age: 37)\n    ]\n    \n    var body: some View {\n        NavigationStack {\n            List(students, id: \\.id) { i in\n                NavigationLink(\"Select \\(i.name)\", value: i)\n            }.navigationDestination(for: Student.self) { student in\n                Text(\"Name \\(student.name)\")\n                Text(\"Age \\(student.age)\")\n            }\n            \n            List(0..\u003c5) { i in\n                NavigationLink(\"Select \\(i)\", value: i)\n            }.navigationDestination(for: Int.self) { selection in\n                Text(\"Selection \\(selection)\")\n            }\n        }\n    }\n}\n```\n\n## Programmatic navigation with NavigationStack\n\n```swift\nimport SwiftUI\n\nstruct ContentView: View {\n    \n    @State private var path = [Int]()\n    \n    var body: some View {\n        NavigationStack(path: $path){\n            VStack {\n                Button(\"Show 32\"){\n                    path = [32]\n                }\n                Button(\"Show 64\") {\n                    path.append(64)\n                }\n                Button(\"Show 32 then 64\") {\n                    path = [32, 64]\n                }\n            }\n            .navigationDestination(for: Int.self) { selection in\n                Text(\"You selected \\(selection)\")\n            }\n        }\n    }\n} \n```\n\n## Navigating to different data types using NavigationPath\n\n```swift\nstruct ContentView: View {\n    \n    @State private var path = NavigationPath()\n    \n    var body: some View {\n        NavigationStack(path: $path) {\n            List {\n                ForEach(0..\u003c5) { i in\n                    NavigationLink(\"Select Number: \\(i)\", value: i)\n                }\n                \n                ForEach(0..\u003c5) { i in\n                    NavigationLink(\"Select String: \\(i)\", value: String(i))\n                }\n            }\n            .toolbar {\n                Button(\"Push 556\") {\n                    path.append(556)\n                }\n                \n                Button(\"Push Hello\") {\n                    path.append(\"Hello\")\n                }\n            }\n            .navigationDestination(for: Int.self) { selection in\n                Text(\"You selected number \\(selection)\")\n            }\n            .navigationDestination(for: String.self) { selection in\n                Text(\"You selected string \\(selection)\")\n            }\n        }\n    }\n}\n```\n\n## How to make a NavigationStack return to its root view programmatically\n\n```swift\nimport SwiftUI\n\nstruct DetailView: View {\n    var number: Int\n//    @Binding var path: Int\n    @Binding var path: NavigationPath\n\n    var body: some View {\n        NavigationLink(\"Go to Random Number\", value: Int.random(in: 1...1000))\n            .navigationTitle(\"Number: \\(number)\")\n            .toolbar {\n                Button(\"Home\"){\n//                    path.removeAll()\n                    path = NavigationPath()\n                }\n            }\n    }\n}\n\nstruct ContentView: View {\n    \n//    @State private var path = [Int]()\n    @State private var path = NavigationPath()\n    \n    \n    var body: some View {\n        NavigationStack(path: $path) {\n            DetailView(number: 0, path: $path)\n                .navigationDestination(for: Int.self) { i in\n                    DetailView(number: i, path: $path)\n                }\n        }\n        .onChange(of: path) { oldValue, newValue in\n            print(newValue)\n        }\n    }\n}\n\n```\n\n## How to save NavigationStack paths using Codable\n\n```swift\nimport SwiftUI\n\n@Observable\nclass PathStore {\n    \n    var path: NavigationPath {\n        didSet {\n            save()\n        }\n    }\n    \n    private let savePath = URL.documentsDirectory.appending(path: \"SavedPath\")\n    \n    init() {\n        if let data =  try? Data(contentsOf: savePath) {\n            if let decoded = try? JSONDecoder().decode(\n                NavigationPath.CodableRepresentation.self,\n                from: data\n            ) {\n                path = NavigationPath(decoded)\n                return\n            }\n        }\n        \n        path = NavigationPath()\n    }\n    \n    func save() {\n        guard let representation = path.codable else { return }\n        \n        do {\n            let data = try? JSONEncoder().encode(representation)\n            try data?.write(to: savePath)\n        } catch {\n            print(\"Failed to save navigation data\")\n        }\n    }\n    \n}\n\nstruct DetailView: View {\n    var number: Int\n    @Binding var path: NavigationPath\n\n    var body: some View {\n        NavigationLink(\"Go to Random Number\", value: Int.random(in: 1...1000))\n            .navigationTitle(\"Number: \\(number)\")\n            .toolbar {\n                Button(\"Home\"){\n                    path = NavigationPath()\n                }\n            }\n    }\n}\n\nstruct ContentView: View {\n    \n    @State private var pathStore = PathStore()\n    \n    \n    var body: some View {\n        NavigationStack(path: $pathStore.path) {\n            DetailView(number: 0, path: $pathStore.path)\n                .navigationDestination(for: Int.self) { i in\n                    DetailView(number: i, path: $pathStore.path)\n                }\n        }\n    }\n}\n```\n\n## Customizing the navigation bar appearance\n\n```swift\nstruct ContentView: View {\n    \n    var body: some View {\n        NavigationStack {\n            List(0..\u003c100) { i in\n                Text(\"Row \\(i)\")\n            }\n            .navigationTitle(\"Title goes here\")\n            .navigationBarTitleDisplayMode(.inline)\n            .toolbarBackground(.blue)\n            .toolbarColorScheme(.dark)\n            .toolbar(.hidden, for: .navigationBar)\n        }\n    }\n}\n```\n\n## Making your navigation title editable\n\n```swift\nimport SwiftUI\n\nstruct ContentView: View {\n    \n    @State private var title = \"SwiftUI\"\n    \n    var body: some View {\n        NavigationStack {\n            Text(\"Hello, word!\")\n                .navigationTitle($title)\n                .navigationBarTitleDisplayMode(.inline)\n        }\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcuadros-code%2Fproject-9-swiftui-navigation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcuadros-code%2Fproject-9-swiftui-navigation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcuadros-code%2Fproject-9-swiftui-navigation/lists"}