列表涵盖了SwiftUI中的自定义视图

时间:2019-10-20 04:05:30

标签: swift swiftui

当我将“ Grid”(Github URL)放入列表时,SwiftUI出现了问题,下一个单元格将其覆盖。但是,当我展开“文本”时,单元格会变大。

List(0 ..< colorArray.count) { i in
VStack(alignment: .leading, spacing: 8) {
    Text("a")
        .font(.body)
        .multilineTextAlignment(.leading)
        .padding(.horizontal, 10)
        .lineLimit(nil)
    Grid {
        ForEach(self.colorArray[i], id: \.self) { color in
            RoundedRectangle(cornerRadius: 10)
                .foregroundColor(Color(color))
                .frame(width: 80.0, height:80.0)
                .overlay(
                    RoundedRectangle(cornerRadius: 10)
                        .stroke(Color(UIColor.label),
                                lineWidth: 0.5)
            )
        }
    }
    .padding(.horizontal, 8)
    .gridStyle(
        AutoColumnsGridStyle(minItemWidth: 80, itemHeight: 80, hSpacing: 8, vSpacing: 8)
    )
}

}

Screenshot of my current result 如何确保“网格”没有被掩盖,如下图所示。 This is the result I would like to get

1 个答案:

答案 0 :(得分:0)

无法重新创建此代码,因为您的代码不完整,但是可以应用

.aspectRatio(contentMode: .fit)

Grid或整个VStack都应强制使像元具有正确的大小。