How to create navigation links from scrollview content

Hello, I am beginner and have a question about making scrollview content open detail view for the item selected. Each item has an image and image name which are pulled from an array .. Any suggestions how to best handle. This is what I have so far .. code wise ....

struct FishGuideView: View {

    @State var fishes = Fish.fishData

    

    var body: some View {

        VStack {

            Text("Fish Guide")

                .font(.largeTitle)

                .multilineTextAlignment(.center)

                .padding(.horizontal, 2.0)

            

            ScrollView(.horizontal) {

                LazyHStack(spacing: 16.0){

                    ForEach(fishes) { item in

                        VStack {

                            GroupBox {

                                Image(item.fishImage)

                                .resizable()

                                .frame(width: 100, height: 88.0)

                                .clipShape(ContainerRelativeShape())

                            Text(item.fishName)

                                    .font(.caption)

                                    .fontWeight(.semibold)

                                .multilineTextAlignment(.center)

                                .lineLimit(4)

                                .padding(.top)

                            

                            

                        }

                        .frame(width: 150, height: 200.0, alignment: .center)

                                     

                    }

                    }

                    

            } //* -END LAZYVSTACK

        } //* -END SCROLLVIEW

    }

        }

} //* -END STRUCT

Replies

You should use code formatter when you paste code (</> button on the code selection)

struct FishGuideView: View {
    @State var fishes = Fish.fishData
    
    var body: some View {
        VStack {
            Text("Fish Guide")
                .font(.largeTitle)
                .multilineTextAlignment(.center)
                .padding(.horizontal, 2.0)
            
            ScrollView(.horizontal) {
                LazyHStack(spacing: 16.0){
                    ForEach(fishes) { item in
                        VStack {
                            GroupBox {
                                Image(item.fishImage)
                                .resizable()
                                .frame(width: 100, height: 88.0)
                                .clipShape(ContainerRelativeShape())
                            Text(item.fishName)
                                    .font(.caption)
                                    .fontWeight(.semibold)
                                .multilineTextAlignment(.center)
                                .lineLimit(4)
                                .padding(.top)
                            
                            
                        }
                        .frame(width: 150, height: 200.0, alignment: .center)
                                     
                    }
                    }
                    
            } //* -END LAZYVSTACK
        } //* -END SCROLLVIEW
    }
        }
} //* -END STRUCT

You have to embed in navigationView, such as here (take care, I did not test, there may need some minor changes) :

struct FishGuideView: View {
    @State var fishes = Fish.fishData
    
    var body: some View {
        VStack {
            Text("Fish Guide")
                .font(.largeTitle)
                .multilineTextAlignment(.center)
                .padding(.horizontal, 2.0)
            
            NavigationView {
                ScrollView(.horizontal) {
                    LazyHStack(spacing: 16.0){
                        ForEach(fishes) { item in
                            NavigationLink (destination: TheViewToGo()) {  
                            // You have to define the destination view you want to go for each item. 
                            // Note it would be more readable to change item in fish.
                                VStack {
                                    GroupBox {
                                        Image(item.fishImage)
                                            .resizable()
                                            .frame(width: 100, height: 88.0)
                                            .clipShape(ContainerRelativeShape())
                                        Text(item.fishName)
                                            .font(.caption)
                                            .fontWeight(.semibold)
                                            .multilineTextAlignment(.center)
                                            .lineLimit(4)
                                            .padding(.top)
                                        
                                        
                                    }
                                    .frame(width: 150, height: 200.0, alignment: .center)
                                    
                                }
                            }
                        }.navigationTitle("Title")
                        
                    } //* -END LAZYVSTACK
                } //* -END SCROLLVIEW
            }
        }
    }
} //* -END STRUCT