In the app I'm working on I have a details view that uses ScrollView to display a bunch of information. I want to use a List inside that ScrollView to display some specific data. However, you can't use a List inside a ScrollView unless you give use a frame modifier. I want the List to display all of its rows without the list having to scroll, which means I need to know how big the frame must be to display all the rows. Say I give the List a height of 200 (using frame) and there are too many rows to show, then the List would scroll. But since the List is inside a ScrollView I want it to show all of the rows and let the ScrollView make it scrollable.
The only way I can think to do this is to calculate the height of each row and use the total height as the List's height. I've been able to do this using the environment variable defaultMinListRowHeight, but that only works if the height of the row is no larger than the default. In the particular scenario I've mentioned above the rows are very likely to have a height greater than the default.
Is there any way I can calculate exactly what the height of the List must be to display each row?
This is the code I've written to size the List based off the defaultMinListRowHeight:
struct DynamicList<SelectionValue: Identifiable, Content: View>: View {
@Environment(\.defaultMinListRowHeight) private var minRowHeight
var values: [SelectionValue]
@ViewBuilder var content: (SelectionValue) -> Content
var body: some View {
List(values) { value in
content(value)
.lineLimit(1)
}
.listStyle(.plain)
.scrollDisabled(true)
.frame(minHeight: minRowHeight * CGFloat(values.count))
}
}

...Since the List view is incompatible with ScrollView. AListin aScrollViewseems to work for me in my tests, you just have to give it a.frame(height: xxxx).GeometryReaderto get the size you want.