Giter VIP home page Giter VIP logo

Comments (5)

matteopuc avatar matteopuc commented on August 21, 2024

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:

Mar-11-2020 16-41-39

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.

matteopuc avatar matteopuc commented on August 21, 2024

Hi @Faltenreich, any updates on this issue? Thanks for your help.

from swiftui-navigation-stack.

Faltenreich avatar Faltenreich commented on August 21, 2024

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.

matteopuc avatar matteopuc commented on August 21, 2024

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.

Faltenreich avatar Faltenreich commented on August 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.