Reputation: 9018
I have a List in a SplitView and want the List rows heights to scale with the width of the ScrollView. How can I do this in a SwiftUI List.
Not sure if using GeometryReader
is the correct way but it seems to be working.
struct BrowserView: View {
@ObservedObject var controller = FileController.shared
var body: some View {
GeometryReader {geometry in
List(selection: self.$controller.selectedObject) {
ForEach(self.controller.thumbNails, id:\.self) { object in
AssetThumbnail(object: object
).aspectRatio(contentMode: .fit).frame(idealHeight: geometry.size.width * 2 / 3 )
.tag(object.id)
}
}
}
}
}
Upvotes: 1
Views: 994
Reputation: 258385
Actually there is nothing harmful to use GeomentryReader
here, but it looks redundant, because as it is seen from below example .aspectRatio
is enough
struct TestListRowHeightAsWidth: View {
var body: some View {
List {
ForEach(0..<10, id:\.self) { object in
Image(systemName: "\(object).square")
.resizable()
.aspectRatio(contentMode: .fit)
}
}
}
}
Upvotes: 1