【问题标题】:How to scale SwiftUI List row height to match the width of the list如何缩放 SwiftUI 列表行高以匹配列表的宽度
【发布时间】:2020-06-28 06:05:08
【问题描述】:

我在 SplitView 中有一个 List,并且希望 List 行的高度随 ScrollView 的宽度而缩放。如何在 SwiftUI 列表中执行此操作。

不确定使用GeometryReader 是否正确,但它似乎有效。

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)
                    
                }
            }
        }
    }
}

【问题讨论】:

    标签: list swiftui


    【解决方案1】:

    其实在这里使用GeomentryReader并没有什么坏处,但是看起来是多余的,因为从下面的例子中可以看出.aspectRatio就足够了

    struct TestListRowHeightAsWidth: View {
        var body: some View {
            List {
                ForEach(0..<10, id:\.self) { object in
                    Image(systemName: "\(object).square")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                }
            }
        }
    }
    

    【讨论】:

    • 抱歉,这是 macOS - 由于图像不是全宽,因此似乎无法正常工作,因此由于某种原因,行似乎包含高度。
    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 2022-01-25
    • 2021-04-22
    • 2020-11-19
    • 2012-06-29
    • 1970-01-01
    • 2014-04-17
    • 2020-02-03
    相关资源
    最近更新 更多