【问题标题】:SwiftUI show collection of imagesSwiftUI 显示图像集合
【发布时间】:2023-01-09 19:56:24
【问题描述】:

我想像这样显示类似于 UICollectionView 的图像网格

每种颜色代表一个图像。

当我执行以下操作时

struct ContentView: View {
var body: some View {
    ScrollView{
        ForEach(0..<8) { i in
            HStack {
                ForEach(0..<3) { j in
                    ZStack(alignment: .center) {
                        Image("image")
                            .background(Color.random)
                            .clipped()
                            .aspectRatio(contentMode: .fill)
                    }
                    .frame(width: 120, height: 120)
                    .background(Color.random)
                }
            }
        }
    }
}

}

我得到一张不受其框架约束的图像,因此填满了整个屏幕。

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    为您的图像设置框架和可调整大小的修饰符。

    Image("image")
     .resizable()
     .frame(width: 120, height: 120)
    

    完整示例

    struct ContentView: View {
    
        var body: some View {
            ScrollView{
                ForEach(0..<8) { i in
                    HStack {
                        ForEach(0..<3) { j in
                            ZStack(alignment: .center) {
                                Image("image")
                                    .resizable()
                                    .frame(width: 120, height: 120)
                            }
                            .frame(width: 120, height: 120)
                            .background(Color.blue)
                        }
                    }
                }
            }
        }
    }
    

    产生这个

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 2021-09-26
      • 2020-10-27
      • 1970-01-01
      • 2020-03-23
      • 2021-12-22
      • 2014-03-21
      • 1970-01-01
      相关资源
      最近更新 更多