Comments (5)
Hi @Faltenreich, thanks for your support. I've just managed to try the NavigationStackView
on iOS 13.0 and I can't manage to reproduce your issue. I tried with this minimum example:
import SwiftUI
import NavigationStack
struct ContentView : View {
var body: some View {
NavigationStackView {
View1()
}
}
}
struct View1: View {
var body: some View {
ZStack {
Color.green.edgesIgnoringSafeArea(.all)
PushView(destination: View2()) {
Text("PUSH")
}
}
}
}
struct View2: View {
@EnvironmentObject var navStack: NavigationStack
var body: some View {
ZStack {
Color.yellow.edgesIgnoringSafeArea(.all)
Button(action: {
self.navStack.pop()
}, label: {
Text("POP")
})
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The example exploits both a PushView
and a programmatic transition to trigger a pop operation. And it works as expected:
Can you provide me with a minimum viable example that causes the problem in order for me to help you debugging this issue? Thank you again.
Matteo
from swiftui-navigation-stack.
Hi @Faltenreich, any updates on this issue? Thanks for your help.
from swiftui-navigation-stack.
Hi @matteopuc, thank you for your quick response!
Our code looks something like that:
import SwiftUI
import NavigationStack
struct ContentView: View {
@EnvironmentObject private var navigationStack: NavigationStack
@ObservedObject private var viewModel = ContentViewModel()
var body: some View {
NavigationStackView(transitionType: .none) {
viewModel.getContentView()
.frame(minWidth: .zero, maxWidth: .infinity, minHeight: .zero, maxHeight: .infinity)
.transition(.slide)
}
}
}
The ContentView itself is nested within some more Views. It contains one dynamic View that is conditionally generated through the ContentViewModel as @published property. Any other View is pushed via the NavigationStack.push() method which is accessed via the EnvironmentObject.
Sadly I am not able to give you a full minimum viable example, since I am currently not at work and have no Apple hardware on my own in order to test it thoroughly.
from swiftui-navigation-stack.
Hi @Faltenreich, thanks for replying and for posting this snippet that shows the issue. The problem is that: in SwiftUI you can access an @EnvironmentObject
property only within the view hierarchy where the environment object has been injected. In this case you are trying to access the navigation stack
@EnvironmentObject private var navigationStack: NavigationStack
outside the navigation stack hierarchy, which begins with:
NavigationStackView(transitionType: .none) {
viewModel.getContentView()
.frame(minWidth: .zero, maxWidth: .infinity, minHeight: .zero, maxHeight: .infinity)
.transition(.slide)
}
You just cannot access the navigation stack outside the hierarchy created by the NavigationStackView
. You need to wrap your content view in a NavigationStackView
, something like:
struct MyRootView: View {
var body: some View {
NavigationStackView {
ContentView()
}
}
}
struct ContentView : View {
@EnvironmentObject private var navigationStack: NavigationStack
@ObservedObject private var viewModel = ContentViewModel()
var body: some View {
viewModel.getContentView()
.frame(minWidth: .zero, maxWidth: .infinity, minHeight: .zero, maxHeight: .infinity)
.transition(.slide)
}
}
Now in your content view you can access the navigation stack.
If you want you can even use the NavigationStackView
as your very root just changing a little bit the SceneDelegate
file:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Create the SwiftUI view that provides the window contents.
let contentView = NavigationStackView {
ContentView()
}
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
from swiftui-navigation-stack.
Hi @matteopuc,
you are absolutely right: this seems like a bug on our side. The EnvironmentObject within the ContentView even seems a bit useless, since it is not used at all. Does the declaration itself lead to this crash - and why only on iOS 13.0 and not on 13.1+?
Either way, I will close this issue and thank you for your engagement. Your library is highly appreciated! :)
from swiftui-navigation-stack.
Related Issues (20)
- Stack Reset / Jump to a completely different view HOT 2
- Transition animation not working HOT 1
- @State variables get reset after pop HOT 3
- Support for iOS 15. HOT 1
- Push doesn't work if app is backgrounded and aware of scenePhase
- Return to the same position in the previous view HOT 2
- Support for Xcode 14 and new SwiftUI API HOT 6
- Previous view completely reloaded after pop HOT 1
- Popping to a specific view
- Callback navigation sometimes doesnt work HOT 1
- iOS 16 transitions malfunctioning HOT 9
- after pop,the previous scrollview auto scroll to top? HOT 6
- Xcode 14 Compiling Issue HOT 6
- not support swipe back HOT 2
- Referencing @Environment(\.scenePhase) var scenePhase breaks navigation when moving app to background
- [Bug]: Clicking on any other window in macOS will return stack to its root
- Value not changed by @Binding
- onappear excuse only first time
- how use in List cell
- after pop,the previous scrollview auto scroll to top?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftui-navigation-stack.