Search code examples
iosswiftscrollswiftuiswiftui-navigationlink

How to make data in NavigationLink scrollable swiftui


Im a beginner in swift, trying to create an app with two screens. Screen A with a list of data. When user selects an item from list the item details will show in Screen B. Thats All.

I used below code. everything is working fine. when i select item in list, screen B displays with data. But the issue is data in screen B is not scrollable.

Here im using a NavigationLink to display data. Tried adding a ScrollView but does not work.

Please let me know what change required to make content in NavigationLink scrollable.

import SwiftUI
struct ContentView: View {
var body: some View {

    NavigationView() {
        VStack{
            List(modelDisease) { myList in

                ScrollView(.vertical,showsIndicators: true){
                    VStack{
                NavigationLink(
            destination: Text( myList.data) .padding()) {
        HStack {
            Text("\(myList.idValue)").frame(width: 50, height: 10, alignment: .leading)
            VStack {
                Text(myList.name)
            }
        }.font(.title)
                }
                }
                }
    }
    }.navigationBarTitle("Ayurvedic Remedies")
    }
}

}


Solution

  • As suggested in the comments, you probably want your destination to be its own view. Right now, you have a List and a ScrollView, which is redundant, so you probably want that ScrollView as part of the destination. It might look something like this (based on your original code, which is missing the modelDisease type and declaration):

    struct ContentView: View {
        //modelDisease must be declared here
        
        var body: some View {
            NavigationView {
                List(modelDisease) { myList in
                    NavigationLink(
                        destination: DestinationView(data: myList.data)) {
                        HStack {
                            Text("\(myList.idValue)").frame(width: 50, height: 10, alignment: .leading)
                            VStack {
                                Text(myList.name)
                            }
                        }.font(.title)
                    }
                }
            }.navigationBarTitle("Ayurvedic Remedies")
        }
    }
    
    
    struct DestinationView : View {
        var data : String
        
        var body: some View {
            ScrollView {
                Text(data)
            }.padding()
        }
    }