erikdrobne / SwiftUICoordinator

SwiftUICoordinator is a package that seamlessly integrates the Coordinator pattern into the SwiftUI framework.
MIT License
146 stars 7 forks source link

Hide back button during navigation #41

Closed nunocaseiro closed 3 months ago

nunocaseiro commented 4 months ago

Hello,

Thanks for sharing this great lib. How can I hide the back navigation button and the title during the navigation?

I have a navbar modifier in swifui which has .navigationBarBackButtonHidden(true) and it doesn't work during the transition

Best regards

erikdrobne commented 4 months ago

Hey @nunocaseiro, thanks for reaching out. Can you maybe share a specific SwiftUI code example?

nunocaseiro commented 4 months ago

Hey @erikdrobne, this scenario is simple to recreate. I also used your example project to test if the behavior is the same.

So I have a view A and a view B. In both views I added the .navigationBarBackButtonHidden(true) and I always see the back button during the push animation.

Can you help me?

Thank you

erikdrobne commented 4 months ago

@nunocaseiro I would suggest using .navigationBarHidden(true) which will also hide it during the transition.

You can take a look here:

https://github.com/user-attachments/assets/947f5810-0ec3-418b-8241-30b125786673

nunocaseiro commented 4 months ago

@erikdrobne Yeah, but I have a custom navigation bar with a custom back button and I don't want to hide it :D

Do you have another suggestion?

erikdrobne commented 4 months ago

@nunocaseiro

Can you add this piece of code to the RouteHostingController and check whether it works for you?

public override func viewWillAppear(_ animated: Bool) {
     super.viewWillAppear(animated)
     // Hide the back button
    self.navigationItem.setHidesBackButton(true, animated: true)
}

public override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // Show the back button again when this view is about to disappear
    self.navigationItem.setHidesBackButton(false, animated: true)
}
nunocaseiro commented 4 months ago

Yes, it works! Keep up your excellent work

It's really useful, thank you

erikdrobne commented 4 months ago

@nunocaseiro If you have time, you can add a route parameter to hide the back button during navigation. Then, submit a PR to close this issue.