mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
hanzoskills.com/llms.txt now opens with the install line, the API reference index and the company index, so an agent that lands on any one of the three can find the rest. robots.txt claimed 537 documents while llms.txt counts 920. The number is gone from robots.txt; llms.txt derives it. public/ is regenerated from scripts/site.py, which it had fallen behind: the copy-prompt menu and agent bar were in the generator and not in the committed pages. tests/site.mjs passes against the result. Co-authored-by: Hanzo Dev <[email protected]>
845 lines
30 KiB
HTML
845 lines
30 KiB
HTML
<!doctype html>
|
|
<html lang="en"><head>
|
|
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>swiftui-navigation</title>
|
|
<meta name="color-scheme" content="dark">
|
|
<style>
|
|
/* Committed to one visual world: a dark instrument panel. No light variant —
|
|
the dot field and the hairline rules only read on black, and a naive
|
|
inversion would wash both out. Every colour is painted explicitly so the page
|
|
never borrows a host background. */
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
|
|
|
|
:root{
|
|
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
|
|
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
|
|
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
|
|
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
|
|
--font-zen-pixel-square:'Zen Pixel Square',monospace;
|
|
--mono:var(--font-zen-mono);
|
|
--sans:var(--font-zen-sans);
|
|
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
|
|
--r:999px; --r-soft:14px;
|
|
}
|
|
*{box-sizing:border-box}
|
|
html{-webkit-text-size-adjust:100%}
|
|
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
|
|
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
|
|
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
|
|
|
|
/* The dot field. A halftone made of one repeating radial-gradient, faded out
|
|
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
|
|
/* No overflow clip here: the dot field is inset to the box already, and a clip
|
|
would cut off the copy-prompt menu. */
|
|
.hero{position:relative;border-bottom:1px solid var(--line)}
|
|
.hero::before{content:"";position:absolute;inset:0;
|
|
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
|
|
background-size:7px 7px;
|
|
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
pointer-events:none}
|
|
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
|
|
|
|
nav{border-bottom:1px solid var(--line)}
|
|
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
|
|
justify-content:space-between;align-items:center;gap:1rem}
|
|
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--ink);text-decoration:none;
|
|
display:inline-flex;align-items:center;gap:.6rem}
|
|
.brand i{font-style:normal;color:var(--faint)}
|
|
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
|
|
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--soft);text-decoration:none}
|
|
nav a.up:hover{color:var(--ink)}
|
|
|
|
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
|
|
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
|
|
h1 em{font-style:normal;color:var(--faint)}
|
|
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
|
|
|
|
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
|
|
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
|
|
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
|
|
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
|
|
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
|
|
line-height:1.2;font-weight:400}
|
|
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
|
|
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
|
|
.btn.solid:hover{background:#d8d8d4}
|
|
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
|
|
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
|
|
|
|
/* Copy-prompt selector. Each row copies the prompt for its own target. */
|
|
.copy{position:relative;display:inline-flex}
|
|
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
|
|
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
|
|
padding:.4rem;display:flex;flex-direction:column;
|
|
box-shadow:0 12px 32px rgba(0,0,0,.75)}
|
|
.menu[hidden]{display:none}
|
|
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
|
|
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
|
|
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
|
|
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
|
|
white-space:nowrap}
|
|
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
|
|
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
|
|
.mi:hover .mark{color:var(--ink)}
|
|
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
|
|
letter-spacing:.1em;text-transform:uppercase}
|
|
|
|
/* Works-with bar: one scrolling row, name and mark per agent. */
|
|
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
|
|
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
|
|
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
|
|
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
|
|
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
|
|
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
|
|
display:flex;align-items:center}
|
|
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
|
|
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
|
|
-webkit-overflow-scrolling:touch;
|
|
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
|
|
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
|
|
.bar::-webkit-scrollbar{display:none}
|
|
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
|
|
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
|
|
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
|
|
text-decoration:none;background:var(--ground)}
|
|
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
|
|
.agent .mark{width:.95rem;height:.95rem}
|
|
|
|
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
|
|
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
|
|
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
|
|
p{margin:0 0 1rem;max-width:70ch}
|
|
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
|
|
li{margin:.22rem 0}
|
|
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
|
|
text-decoration-color:var(--line-hi)}
|
|
a:hover{text-decoration-color:var(--ink)}
|
|
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
|
|
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
|
|
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
|
|
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
|
|
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
|
|
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
|
|
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
|
|
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
|
|
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
|
|
display:block;overflow-x:auto}
|
|
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
|
|
|
|
.curl{display:block;background:var(--panel);border:1px solid var(--line);
|
|
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
|
|
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
|
|
white-space:pre;line-height:1.9}
|
|
.curl b{color:var(--ink);font-weight:400}
|
|
.curl i{font-style:normal;color:var(--faint)}
|
|
|
|
/* index */
|
|
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
|
|
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
|
|
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
|
|
#q:focus{outline:none;border-color:var(--soft)}
|
|
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
|
|
text-transform:uppercase;margin:0 0 2rem}
|
|
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
|
|
padding-bottom:.45rem}
|
|
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
|
|
border-radius:var(--r-soft);text-decoration:none}
|
|
.row:hover{background:#0a0a0a}
|
|
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
|
|
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
|
|
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
|
|
color:var(--faint);font-size:.85rem}
|
|
footer a{color:var(--soft)}
|
|
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
|
|
margin:0 0 2rem;font-size:.85rem}
|
|
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
|
|
border-bottom:1px solid var(--line);padding-bottom:.4rem}
|
|
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
|
|
.fmap a:hover{text-decoration:underline}
|
|
.fmap div{color:var(--faint);line-height:1.5}
|
|
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
|
|
|
|
@media (max-width:640px){
|
|
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
|
|
.btns{gap:.5rem}
|
|
.btn{padding:.75rem 1.15rem;font-size:.68rem}
|
|
.copy,.copy .btn{width:100%;justify-content:center}
|
|
.menu{left:0;right:0;min-width:0}
|
|
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
|
|
.strip b{border-right:0;padding-right:0}
|
|
.bar{width:100%}
|
|
}
|
|
</style>
|
|
<link rel="alternate" type="text/markdown" href="/swiftui-navigation.md">
|
|
|
|
<link rel="icon" href="/favicon.svg">
|
|
<link rel="canonical" href="https://hanzoskills.com/swiftui-navigation">
|
|
</head>
|
|
<!-- Reading this with a machine? Every page here is also plain markdown.
|
|
This one: https://hanzoskills.com/swiftui-navigation.md
|
|
Index: https://hanzoskills.com/llms.txt
|
|
Corpus: https://hanzoskills.com/all.md
|
|
Append .md to any path for the markdown twin. -->
|
|
<body>
|
|
<nav><div class="in">
|
|
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
|
|
<a class="up" href="/">All skills</a>
|
|
</div></nav>
|
|
|
|
<div class="wrap">
|
|
<h1>swiftui-navigation</h1><p class="lede">Implementing navigation in iOS 17+ apps</p><h1>SwiftUI Navigation Patterns</h1>
|
|
<p><strong>Use this skill when:</strong></p>
|
|
<ul><li>Implementing navigation in iOS 17+ apps</li><li>Building type-safe navigation flows</li><li>Managing navigation state programmatically</li><li>Creating deep linking and universal links</li><li>Handling complex navigation hierarchies</li></ul>
|
|
<h2>NavigationStack Fundamentals</h2>
|
|
<h3>Basic Navigation</h3>
|
|
<p>Use <code>NavigationStack</code> for hierarchical navigation:</p>
|
|
<pre><code class="lang-swift">struct RootView: View {
|
|
var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
NavigationLink("Settings") {
|
|
SettingsView()
|
|
}
|
|
|
|
NavigationLink("Profile") {
|
|
ProfileView()
|
|
}
|
|
}
|
|
.navigationTitle("Home")
|
|
}
|
|
}
|
|
}</code></pre>
|
|
<h3>Programmatic Navigation</h3>
|
|
<p>Control navigation with path binding:</p>
|
|
<pre><code class="lang-swift">@Observable
|
|
final class NavigationCoordinator {
|
|
var path: NavigationPath = NavigationPath()
|
|
|
|
func navigateToProfile() {
|
|
path.append(Route.profile)
|
|
}
|
|
|
|
func navigateToSettings() {
|
|
path.append(Route.settings)
|
|
}
|
|
|
|
func popToRoot() {
|
|
path.removeLast(path.count)
|
|
}
|
|
|
|
func goBack() {
|
|
if !path.isEmpty {
|
|
path.removeLast()
|
|
}
|
|
}
|
|
}
|
|
|
|
enum Route: Hashable {
|
|
case profile
|
|
case settings
|
|
case userDetail(userId: UUID)
|
|
case editTask(taskId: UUID)
|
|
}
|
|
|
|
struct ContentView: View {
|
|
@State private var coordinator = NavigationCoordinator()
|
|
|
|
var body: some View {
|
|
NavigationStack(path: $coordinator.path) {
|
|
HomeView()
|
|
.navigationDestination(for: Route.self) { route in
|
|
switch route {
|
|
case .profile:
|
|
ProfileView()
|
|
case .settings:
|
|
SettingsView()
|
|
case .userDetail(let userId):
|
|
UserDetailView(userId: userId)
|
|
case .editTask(let taskId):
|
|
TaskEditorView(taskId: taskId)
|
|
}
|
|
}
|
|
}
|
|
.environment(coordinator)
|
|
}
|
|
}
|
|
|
|
// Usage in child view
|
|
struct HomeView: View {
|
|
@Environment(NavigationCoordinator.self) private var coordinator
|
|
|
|
var body: some View {
|
|
Button("Go to Profile") {
|
|
coordinator.navigateToProfile()
|
|
}
|
|
}
|
|
}</code></pre>
|
|
<h2>Type-Safe Navigation</h2>
|
|
<h3>Value-Based Navigation</h3>
|
|
<p>Navigate with type-safe routes:</p>
|
|
<pre><code class="lang-swift">// Define navigation routes
|
|
enum AppRoute: Hashable {
|
|
case home
|
|
case userList
|
|
case userDetail(User)
|
|
case taskList(projectId: UUID)
|
|
case taskDetail(Task)
|
|
case settings
|
|
}
|
|
|
|
@Observable
|
|
final class AppNavigator {
|
|
var path: [AppRoute] = []
|
|
|
|
func navigate(to route: AppRoute) {
|
|
path.append(route)
|
|
}
|
|
|
|
func navigateToUser(_ user: User) {
|
|
path.append(.userDetail(user))
|
|
}
|
|
|
|
func navigateToTask(_ task: Task) {
|
|
path.append(.taskDetail(task))
|
|
}
|
|
|
|
func pop() {
|
|
if !path.isEmpty {
|
|
path.removeLast()
|
|
}
|
|
}
|
|
|
|
func popToRoot() {
|
|
path.removeAll()
|
|
}
|
|
|
|
func popTo(_ route: AppRoute) {
|
|
guard let index = path.firstIndex(of: route) else { return }
|
|
path.removeLast(path.count - index - 1)
|
|
}
|
|
}
|
|
|
|
struct AppView: View {
|
|
@State private var navigator = AppNavigator()
|
|
|
|
var body: some View {
|
|
NavigationStack(path: $navigator.path) {
|
|
RootView()
|
|
.navigationDestination(for: AppRoute.self) { route in
|
|
routeView(for: route)
|
|
}
|
|
}
|
|
.environment(navigator)
|
|
}
|
|
|
|
@ViewBuilder
|
|
private func routeView(for route: AppRoute) -> some View {
|
|
switch route {
|
|
case .home:
|
|
HomeView()
|
|
case .userList:
|
|
UserListView()
|
|
case .userDetail(let user):
|
|
UserDetailView(user: user)
|
|
case .taskList(let projectId):
|
|
TaskListView(projectId: projectId)
|
|
case .taskDetail(let task):
|
|
TaskDetailView(task: task)
|
|
case .settings:
|
|
SettingsView()
|
|
}
|
|
}
|
|
}</code></pre>
|
|
<h3>NavigationPath for Mixed Types</h3>
|
|
<p>Handle heterogeneous navigation paths:</p>
|
|
<pre><code class="lang-swift">struct MixedNavigationView: View {
|
|
@State private var path = NavigationPath()
|
|
|
|
var body: some View {
|
|
NavigationStack(path: $path) {
|
|
List {
|
|
Button("Show String") {
|
|
path.append("Hello")
|
|
}
|
|
|
|
Button("Show Number") {
|
|
path.append(42)
|
|
}
|
|
|
|
Button("Show User") {
|
|
path.append(User.sample)
|
|
}
|
|
}
|
|
.navigationDestination(for: String.self) { text in
|
|
Text("String: \(text)")
|
|
}
|
|
.navigationDestination(for: Int.self) { number in
|
|
Text("Number: \(number)")
|
|
}
|
|
.navigationDestination(for: User.self) { user in
|
|
UserDetailView(user: user)
|
|
}
|
|
}
|
|
}
|
|
}</code></pre>
|
|
<h2>Deep Linking and URL Handling</h2>
|
|
<h3>URL-Based Navigation</h3>
|
|
<p>Handle deep links and universal links:</p>
|
|
<pre><code class="lang-swift">@Observable
|
|
final class DeepLinkHandler {
|
|
var path: NavigationPath = NavigationPath()
|
|
|
|
func handle(_ url: URL) {
|
|
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
|
|
return
|
|
}
|
|
|
|
// Parse URL: myapp://user/123
|
|
let pathComponents = components.path.components(separatedBy: "/")
|
|
|
|
switch pathComponents.first {
|
|
case "user":
|
|
if let userId = pathComponents.dropFirst().first,
|
|
let uuid = UUID(uuidString: userId) {
|
|
path.append(Route.userDetail(userId: uuid))
|
|
}
|
|
|
|
case "task":
|
|
if let taskId = pathComponents.dropFirst().first,
|
|
let uuid = UUID(uuidString: taskId) {
|
|
path.append(Route.taskDetail(taskId: uuid))
|
|
}
|
|
|
|
case "settings":
|
|
path.append(Route.settings)
|
|
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
@main
|
|
struct MyApp: App {
|
|
@State private var deepLinkHandler = DeepLinkHandler()
|
|
|
|
var body: some Scene {
|
|
WindowGroup {
|
|
NavigationStack(path: $deepLinkHandler.path) {
|
|
HomeView()
|
|
.navigationDestination(for: Route.self) { route in
|
|
routeView(for: route)
|
|
}
|
|
}
|
|
.onOpenURL { url in
|
|
deepLinkHandler.handle(url)
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private func routeView(for route: Route) -> some View {
|
|
// Route implementation
|
|
}
|
|
}</code></pre>
|
|
<h3>Query Parameter Handling</h3>
|
|
<p>Parse URL query parameters:</p>
|
|
<pre><code class="lang-swift">extension DeepLinkHandler {
|
|
func handleWithQuery(_ url: URL) {
|
|
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
|
|
return
|
|
}
|
|
|
|
// Parse: myapp://search?q=swift&category=ios
|
|
let queryItems = components.queryItems ?? []
|
|
|
|
if components.path == "/search" {
|
|
let query = queryItems.first { $0.name == "q" }?.value ?? ""
|
|
let category = queryItems.first { $0.name == "category" }?.value
|
|
|
|
path.append(Route.search(query: query, category: category))
|
|
}
|
|
}
|
|
}</code></pre>
|
|
<h2>Tab-Based Navigation</h2>
|
|
<h3>NavigationStack in Tabs</h3>
|
|
<p>Combine tabs with navigation:</p>
|
|
<pre><code class="lang-swift">struct TabNavigationView: View {
|
|
@State private var selectedTab = 0
|
|
@State private var homePath = NavigationPath()
|
|
@State private var searchPath = NavigationPath()
|
|
@State private var profilePath = NavigationPath()
|
|
|
|
var body: some View {
|
|
TabView(selection: $selectedTab) {
|
|
NavigationStack(path: $homePath) {
|
|
HomeView()
|
|
.navigationDestination(for: Route.self) { route in
|
|
routeView(for: route)
|
|
}
|
|
}
|
|
.tabItem {
|
|
Label("Home", systemImage: "house")
|
|
}
|
|
.tag(0)
|
|
|
|
NavigationStack(path: $searchPath) {
|
|
SearchView()
|
|
.navigationDestination(for: Route.self) { route in
|
|
routeView(for: route)
|
|
}
|
|
}
|
|
.tabItem {
|
|
Label("Search", systemImage: "magnifyingglass")
|
|
}
|
|
.tag(1)
|
|
|
|
NavigationStack(path: $profilePath) {
|
|
ProfileView()
|
|
.navigationDestination(for: Route.self) { route in
|
|
routeView(for: route)
|
|
}
|
|
}
|
|
.tabItem {
|
|
Label("Profile", systemImage: "person")
|
|
}
|
|
.tag(2)
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private func routeView(for route: Route) -> some View {
|
|
// Shared route handling
|
|
}
|
|
}</code></pre>
|
|
<h3>Preserving Tab State</h3>
|
|
<p>Maintain navigation state per tab:</p>
|
|
<pre><code class="lang-swift">@Observable
|
|
final class TabCoordinator {
|
|
var selectedTab: Tab = .home
|
|
var homePath: [Route] = []
|
|
var searchPath: [Route] = []
|
|
var profilePath: [Route] = []
|
|
|
|
var currentPath: Binding<[Route]> {
|
|
switch selectedTab {
|
|
case .home:
|
|
return Binding(
|
|
get: { self.homePath },
|
|
set: { self.homePath = $0 }
|
|
)
|
|
case .search:
|
|
return Binding(
|
|
get: { self.searchPath },
|
|
set: { self.searchPath = $0 }
|
|
)
|
|
case .profile:
|
|
return Binding(
|
|
get: { self.profilePath },
|
|
set: { self.profilePath = $0 }
|
|
)
|
|
}
|
|
}
|
|
|
|
func navigate(to route: Route) {
|
|
switch selectedTab {
|
|
case .home:
|
|
homePath.append(route)
|
|
case .search:
|
|
searchPath.append(route)
|
|
case .profile:
|
|
profilePath.append(route)
|
|
}
|
|
}
|
|
|
|
func popToRoot() {
|
|
switch selectedTab {
|
|
case .home:
|
|
homePath.removeAll()
|
|
case .search:
|
|
searchPath.removeAll()
|
|
case .profile:
|
|
profilePath.removeAll()
|
|
}
|
|
}
|
|
}
|
|
|
|
enum Tab {
|
|
case home, search, profile
|
|
}</code></pre>
|
|
<h2>Modal Presentation</h2>
|
|
<h3>Sheet Presentation</h3>
|
|
<p>Present modal views:</p>
|
|
<pre><code class="lang-swift">struct ContentView: View {
|
|
@State private var showSettings = false
|
|
@State private var selectedUser: User?
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
Button("Show Settings") {
|
|
showSettings = true
|
|
}
|
|
|
|
ForEach(users) { user in
|
|
Button(user.name) {
|
|
selectedUser = user
|
|
}
|
|
}
|
|
}
|
|
.sheet(isPresented: $showSettings) {
|
|
SettingsView()
|
|
}
|
|
.sheet(item: $selectedUser) { user in
|
|
UserDetailSheet(user: user)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Dismissable sheet
|
|
struct UserDetailSheet: View {
|
|
@Environment(\.dismiss) private var dismiss
|
|
let user: User
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
UserDetailView(user: user)
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("Done") {
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}</code></pre>
|
|
<h3>Full Screen Cover</h3>
|
|
<p>Present full-screen modals:</p>
|
|
<pre><code class="lang-swift">struct MainView: View {
|
|
@State private var showOnboarding = false
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
ContentView()
|
|
}
|
|
.fullScreenCover(isPresented: $showOnboarding) {
|
|
OnboardingView()
|
|
}
|
|
.onAppear {
|
|
showOnboarding = !hasCompletedOnboarding
|
|
}
|
|
}
|
|
}</code></pre>
|
|
<h3>Alert and Confirmation Dialog</h3>
|
|
<p>Show simple dialogs:</p>
|
|
<pre><code class="lang-swift">struct ActionView: View {
|
|
@State private var showDeleteAlert = false
|
|
@State private var showOptions = false
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Button("Delete") {
|
|
showDeleteAlert = true
|
|
}
|
|
.alert("Delete Item?", isPresented: $showDeleteAlert) {
|
|
Button("Cancel", role: .cancel) { }
|
|
Button("Delete", role: .destructive) {
|
|
deleteItem()
|
|
}
|
|
} message: {
|
|
Text("This action cannot be undone.")
|
|
}
|
|
|
|
Button("Options") {
|
|
showOptions = true
|
|
}
|
|
.confirmationDialog("Choose Action", isPresented: $showOptions) {
|
|
Button("Edit") { editItem() }
|
|
Button("Share") { shareItem() }
|
|
Button("Delete", role: .destructive) { deleteItem() }
|
|
Button("Cancel", role: .cancel) { }
|
|
}
|
|
}
|
|
}
|
|
|
|
private func deleteItem() { }
|
|
private func editItem() { }
|
|
private func shareItem() { }
|
|
}</code></pre>
|
|
<h2>State Restoration</h2>
|
|
<h3>Preserving Navigation State</h3>
|
|
<p>Save and restore navigation:</p>
|
|
<pre><code class="lang-swift">@Observable
|
|
final class NavigationState {
|
|
var path: [Route] = []
|
|
|
|
func save() {
|
|
let encoder = JSONEncoder()
|
|
if let data = try? encoder.encode(path) {
|
|
UserDefaults.standard.set(data, forKey: "navigation_path")
|
|
}
|
|
}
|
|
|
|
func restore() {
|
|
guard let data = UserDefaults.standard.data(forKey: "navigation_path"),
|
|
let restored = try? JSONDecoder().decode([Route].self, from: data) else {
|
|
return
|
|
}
|
|
|
|
path = restored
|
|
}
|
|
}
|
|
|
|
@main
|
|
struct MyApp: App {
|
|
@State private var navigationState = NavigationState()
|
|
|
|
var body: some Scene {
|
|
WindowGroup {
|
|
NavigationStack(path: $navigationState.path) {
|
|
HomeView()
|
|
.navigationDestination(for: Route.self) { route in
|
|
routeView(for: route)
|
|
}
|
|
}
|
|
.onChange(of: navigationState.path) { oldPath, newPath in
|
|
navigationState.save()
|
|
}
|
|
.onAppear {
|
|
navigationState.restore()
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private func routeView(for route: Route) -> some View {
|
|
// Route implementation
|
|
}
|
|
}</code></pre>
|
|
<h2>Testing Navigation</h2>
|
|
<h3>Testing Navigation Flows</h3>
|
|
<p>Test navigation logic:</p>
|
|
<pre><code class="lang-swift">import Testing
|
|
|
|
@Test
|
|
func testNavigationToProfile() {
|
|
let navigator = AppNavigator()
|
|
|
|
navigator.navigateToProfile()
|
|
|
|
#expect(navigator.path.count == 1)
|
|
#expect(navigator.path.first == .profile)
|
|
}
|
|
|
|
@Test
|
|
func testPopToRoot() {
|
|
let navigator = AppNavigator()
|
|
|
|
navigator.navigate(to: .userList)
|
|
navigator.navigate(to: .userDetail(User.sample))
|
|
navigator.navigate(to: .settings)
|
|
|
|
#expect(navigator.path.count == 3)
|
|
|
|
navigator.popToRoot()
|
|
|
|
#expect(navigator.path.isEmpty)
|
|
}
|
|
|
|
@Test
|
|
func testDeepLinkParsing() {
|
|
let handler = DeepLinkHandler()
|
|
let url = URL(string: "myapp://user/\(UUID().uuidString)")!
|
|
|
|
handler.handle(url)
|
|
|
|
#expect(handler.path.count == 1)
|
|
}</code></pre>
|
|
<h2>Anti-Patterns to Avoid</h2>
|
|
<p><strong>DON'T use NavigationView (deprecated):</strong></p>
|
|
<pre><code class="lang-swift">// ❌ BAD - Deprecated in iOS 16
|
|
NavigationView {
|
|
List { }
|
|
}
|
|
|
|
// ✅ GOOD
|
|
NavigationStack {
|
|
List { }
|
|
}</code></pre>
|
|
<p><strong>DON'T manage multiple NavigationPath states manually:</strong></p>
|
|
<pre><code class="lang-swift">// ❌ BAD - Hard to maintain
|
|
@State private var path1 = NavigationPath()
|
|
@State private var path2 = NavigationPath()
|
|
@State private var path3 = NavigationPath()
|
|
|
|
// ✅ GOOD - Use coordinator
|
|
@State private var coordinator = TabCoordinator()</code></pre>
|
|
<p><strong>DON'T ignore dismiss environment:</strong></p>
|
|
<pre><code class="lang-swift">// ❌ BAD - Custom dismissal logic
|
|
@State private var isPresented = false
|
|
|
|
Button("Close") {
|
|
isPresented = false // Parent must manage this
|
|
}
|
|
|
|
// ✅ GOOD
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
Button("Close") {
|
|
dismiss() // Works anywhere in hierarchy
|
|
}</code></pre>
|
|
<p><strong>DON'T mix navigation paradigms:</strong></p>
|
|
<pre><code class="lang-swift">// ❌ BAD - Mixing NavigationStack and NavigationLink value
|
|
NavigationStack(path: $path) {
|
|
NavigationLink(value: route) { // Good
|
|
Text("Link 1")
|
|
}
|
|
|
|
NavigationLink(destination: DetailView()) { // Bad - old style
|
|
Text("Link 2")
|
|
}
|
|
}
|
|
|
|
// ✅ GOOD - Consistent approach
|
|
NavigationStack(path: $path) {
|
|
NavigationLink(value: route1) {
|
|
Text("Link 1")
|
|
}
|
|
|
|
NavigationLink(value: route2) {
|
|
Text("Link 2")
|
|
}
|
|
}</code></pre>
|
|
<h2>Related Skills</h2>
|
|
<ul><li><strong>swiftui-architecture.md</strong> - Navigation in MVVM architecture</li><li><strong>swift-concurrency.md</strong> - Async navigation flows</li><li><strong>swiftdata-persistence.md</strong> - Navigating with persisted models</li><li><strong>ios-testing.md</strong> - Testing navigation logic</li></ul>
|
|
<footer>
|
|
<div class="fmap">
|
|
<div><b>Build</b>
|
|
<a href="https://hanzo.app">hanzo.app</a> app builder
|
|
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
|
|
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
|
|
<div><b>Run</b>
|
|
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
|
|
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
|
|
<div><b>Account</b>
|
|
<a href="https://hanzo.id">hanzo.id</a> sign in
|
|
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
|
|
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
|
|
<div><b>Company</b>
|
|
<a href="https://hanzo.ai">hanzo.ai</a> canonical
|
|
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
|
|
</div>
|
|
<div class="fend"><a href="/swiftui-navigation.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
|
|
</footer>
|
|
</div></body></html> |