0

I want to show the detail page on opening the app with back button. On clicking the back button list of accounts should display. Just like the iOS default mail app.

I'm using the NavigationStack, NavigationLink and .navigationDestinatiion

Please refer below gif.

Thanks in advance.

enter image description here

Azhagusundaram Tamil
  • 2,053
  • 3
  • 21
  • 36

1 Answers1

0

You can achieve this with the use of NavigationStack.

Here is the sample code.

class Coordinator: ObservableObject {
    @Published var path: [String] = []
}

struct Testing: View {
    @StateObject var coordinator = Coordinator()
    
    var body: some View {
        NavigationStack(path: $coordinator.path) {
            DefaultView()
        }
        .environmentObject(coordinator)
    }
    
}

struct DefaultView: View {
    @EnvironmentObject var coordinator: Coordinator
    @State private var isDetailViewActive = true
    
    var body: some View {
        Text("Hello world")
            .onAppear {
                if isDetailViewActive == true {
                    self.isDetailViewActive = false
                    coordinator.path.append("DetailsView")
                }
            }
            .navigationDestination(for: String.self) { view in
                DetailsView()
            }
    }
}

struct DetailsView: View {
    var body: some View {
        NavigationView {
            Text("How are you")
        }
    }
}
Faaiz Daag
  • 155
  • 11