You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+41-2Lines changed: 41 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -278,6 +278,47 @@ struct ChildView: View {
278
278
}
279
279
```
280
280
281
+
## NavigationStack injection
282
+
283
+
By default you can programmatically push and pop only inside the `NavigationStackView` hierarchy (by accessing the `NavigationStack` environment object). If you want to use the `NavigationStack` outside the `NavigationStackView` you need to create your own `NavigationStack` (wherever you want) **and pass it as parameter to the `NavigationStackView`**.
284
+
285
+
**Important:** Every `NavigationStack` must be associated to a `NavigationStackView`. A `NavigationStack` cannot be shared between multiple `NavigationStackView`.
286
+
287
+
This is useful when you want to _decouple your routing logic from views by using your own router class_, for example:
288
+
289
+
```
290
+
class MyRouter {
291
+
private let navStack: NavigationStack
292
+
293
+
init(navStack: NavigationStack) {
294
+
self.navStack = navStack
295
+
}
296
+
297
+
func rootView() -> some View {
298
+
RootView()
299
+
}
300
+
301
+
func toLogin() {
302
+
self.navStack.push(LoginScreen())
303
+
}
304
+
305
+
func toSignUp() {
306
+
self.navStack.push(SignUpScreen())
307
+
}
308
+
}
309
+
310
+
struct RootView: View {
311
+
let navStack: NavigationStack
312
+
let router: MyRouter
313
+
314
+
var body: some View {
315
+
NavigationStackView(navigationStack: navStack) {
316
+
router.rootView()
317
+
}
318
+
}
319
+
}
320
+
```
321
+
281
322
## Important
282
323
283
324
Please, note that `NavigationStackView` navigates between views and two views may be smaller than the entire screen. In that case the transition animation won't involve the whole screen, but just the two views. Let's make an example:
@@ -411,5 +452,3 @@ struct MyView: View {
411
452
412
453
SwiftUI is really new, there are some bugs in the framework (or unexpected behaviours) and several API not yet documented. Please, report any issue may arise and feel free to suggest any improvement or changing to this first implementation of a navigation stack.
0 commit comments