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
attaching the easing directly to the transition. **Don't do this**. SwiftUI has still some problems with implicit animations attached to transitions, so it may not work. For example, implicit animations attached to a .slide transition won't work.
87
87
88
-
## NavigationStack injection
89
-
90
-
If you want to use the NavigationStack also outside the views hierachy you need to create you custom NavigationStack and pass it as parameter to the NavigationStackView.
91
-
92
-
This is useful when you want to _decouple your routing logic from views by using your own router class_, for example:
93
-
94
-
```
95
-
class MyRouter {
96
-
let navStack: NavigationStack
97
-
98
-
init(navStack: NavigationStack) {
99
-
self.navStack = navStack
100
-
}
101
-
102
-
func rootView() {
103
-
if userIsLoggedIn() {
104
-
return HomeScreen()
105
-
} else {
106
-
return LoginScreen()
107
-
}
108
-
}
109
-
110
-
func toLogin() {
111
-
self.navStack.push(LoginScreen())
112
-
}
113
-
114
-
//...
115
-
}
116
-
117
-
struct ContentView: View {
118
-
let navStack: NavigationStack
119
-
let router: MyRouter
120
-
121
-
var body: some View {
122
-
NavigationStackView(navigationStack: navStack) {
123
-
router.rootView()
124
-
}
125
-
}
126
-
}
127
-
```
128
-
129
-
Setup the ContentView in your SceneDelegate (or similarly in the App class)
130
-
131
-
```
132
-
// Create the SwiftUI view that provides the window contents.
133
-
let contentView = ContentView(navStack: Injector.shared.navStack,
134
-
router: Injector.shared.router)
135
-
136
-
// Use a UIHostingController as window root view controller.
In order to navigate forward you have two options:
@@ -335,6 +278,47 @@ struct ChildView: View {
335
278
}
336
279
```
337
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
+
338
322
## Important
339
323
340
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:
@@ -468,5 +452,3 @@ struct MyView: View {
468
452
469
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