Duncan Groenewald
Duncan Groenewald

Reputation: 9018

How to scale SwiftUI List row height to match the width of the list

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

Answers (1)

Asperi
Asperi

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

demo

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

Related Questions