How to set tableview height according to its content-size in SwiftUI?

860 views Asked by At

I need to disable scroll in tableView and increase the height according to its content. below is my code for table view in swiftUI please draw me guidance

var body: some View {
        List{

         ForEach(testData){ data in
                    VStack(alignment: .leading){
                                   HStack{
                                    Image(data.name)
                                       .resizable()
                                       .frame(width: 50, height: 50, alignment: .leading)

                                    VStack(alignment: .leading){
                                        Text(data.name)
                                               .bold()
                                        Text(data.bio)
                                               .font(.subheadline)
                                       }
                                   }
                               }
            }
        }
        .offset(y:-60)
        .padding(8)
    }
1

There are 1 answers

0
Ben O On

You could do this by removing the List and swapping it for a VStack. Then you would use a divider at the end of each VStack of elements and you would add horizontal padding and this gives the same effect as a list, but makes it not scrollable and it would adjust height depending on the elements.

VStack{
ForEach(testData){ data in
 VStack(alignment: .leading){
        HStack{
        Image(data.name)
            .resizable()
            .frame(width: 50, height: 50, alignment: .leading)

         VStack(alignment: .leading){
             Text(data.name)
                    .bold()
             Text(data.bio)
                    .font(.subheadline)
            }
        }
    }
 }
Divider()
.padding(.horizontal)
}
.offset(y:-60)
.padding(8)

}