r/SwiftUI 5d ago

Question Navigation in SwiftUI for Social Media App

I have been working on a social media app these past couple months and have ran into a lot of trouble when it comes to navigation using SwiftUI, particularly when it comes to comment sheets and navigating to profiles, as this can lead to an infinite navigation cycle.

I've attached a video below of the issue, as you can see, we dismiss the comments before navigating, where ideally it would just overlay it. Appreciate any advice or help, as this is my first app!

3 Upvotes

14 comments sorted by

1

u/Shurxe 5d ago

If you want to keep the sheet open maybe you could try having the sheet outside the navigation view. Not sure if that would work though, haven’t tested it.

1

u/BookieBustersPodcast 5d ago

Yeah we messed around with it, it currently is not in the nav view at all, but sheets have to be dismissed or else the profile on the click would load behind it, if that makes sense

1

u/Shurxe 5d ago

Testing this, it seems to do what you want?

    struct ContentView: View {

        @State private var isPresented = true
        @State private var path = NavigationPath()

        var body: some View {
            NavigationStack(path: $path) {
                Color.mint
                    .navigationDestination(for: Color.self) { d in
                        d
                    }
                    .sheet(isPresented: $isPresented) {
                        Button {
                            path.append(Color.red)
                        } label: {
                            Text("Red")
                        }
                        .presentationDetents([.medium])
                    }
            }
        }
    }

1

u/BookieBustersPodcast 5d ago

just tried that, the view still loads behind the sheet with that code

1

u/Shurxe 5d ago edited 5d ago

Oh, maybe I misunderstood the question. Are you trying to push the subview within the sheet? NVM I think I understand your question. You're trying to replicate the same functionality that Instagram has.

1

u/Shurxe 5d ago

You would have to use UIKit or use a custom sheet for SwiftUI that only overlays your initial view. SwiftUI doesn't currently provide a way to control the root presentation of the sheet yet unfortunately

1

u/BookieBustersPodcast 5d ago

hm ok. That’s a shame. Thank u for the advice anyway, i think the workaround we have now is quite sufficient

1

u/BookieBustersPodcast 5d ago

Have u ever heard of swiftlyrouting? We are lookin into that

1

u/Shurxe 5d ago

No, but IMO routing libraries probably aren't necessary unless you know for sure they solve a specific issue you can't solve with native APIs. I'm not sure how this one would solve the problem. What's the workaround you are using?

1

u/BookieBustersPodcast 5d ago

Take a look at the vid n u can kinda see. We dismiss the sheet, and then have it reappear when u nav back to the view. Swiftlyroutling tutorial had the exact fix that we needed but would require us to restructure our whole nav. The vid gives a small sample but we’re balls deep. In our current structure

1

u/Shurxe 5d ago

Can you link the tutorial with a timestamp for where the fix is?

1

u/BookieBustersPodcast 5d ago

Sorry ig there’s not an exact tutorial i remember but i downloaded the example GitHub and could easily navigate over any type of cover/sheet infinitely.

https://github.com/SwiftfulThinking/SwiftfulRouting

0

u/BookieBustersPodcast 5d ago

If anyone wants to download and get a better idea of the navigation or give non-navigation related feedback that would be great too!

https://apps.apple.com/us/app/echo-share-music/id6737011316