0

I'm try to navigate through NavigationView and NavigationLink(destination: but it's not working for me.

 ScrollView(.horizontal) {
        LazyHStack {
            
            ForEach(0...10, id: \.self) { index in
                
                ZStack {
                    HListContentView(descriptionLineLimit: 3)
                        .background(.white)
                        .cornerRadius(8)
                        .frame(width: geometry.size.width - 20, height: 200)
                    
                        .onAppear {
                            print("map view: \(index)")
                        }
                        .shadow(color: .gray, radius: 10, x: 0.1, y: 0.1)
                    NavigationLink(destination: DetailsView()) {
                        EmptyView()
                    }
                    
                    
                    .opacity(0)
                    .buttonStyle(PlainButtonStyle())
                }
                
            }
            
        }
 //     }
}

code screen shot

adding my app screen shot with data list and map

James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

1

It can be solved by using below 2 approaches:

First:

NavigationLink("Tap to show detail"){DetailsView()}

Second:

NavigationLink(destination: DetailsView(), isActive: $isShowingDetailsView) {
                    EmptyView()
                    Button("Tap to show detail") {
                        isShowingDetailsView = true
                    }
                }

The advantage to this approach over a simple NavigationLink is that our button can do any amount of other work before triggering the programmatic navigation. e.g. maybe you want to save some data or authenticate the user etc..

Updated

Here is another method without deprecated message

@State private var isShowingDetailsView : Bool = false

    var body: some View {
        NavigationStack {
           VStack {
              Button {
                  //Code here before changing the bool value
                  isShowingDetailsView = true
              } label: {
                  Text(“Tap to show details”)
              }
          }
           .navigationDestination(isPresented: $isShowingDetailsView) {
              DetailsView()
          }
       }
   }
}

But this method can be used only in iOS 16 or above

  • it worked but it adding "Tap to show detail" text on horizontal list by using 1st approach, by 2nd approach it replaceable with "" in Button title and worked fine with ** was deprecated in iOS 16.0**, is any other method without **deprecated** message? – Deepak Singh Dec 12 '22 at 11:24
  • Hi @DeepakSingh Answer is updated with new method without deprecated message. –  Dec 13 '22 at 12:18