0

我正在尝试将网格(使用滚动视图开发)放在滚动视图中,但我无法使用内容高度设置网格高度。我无法处理这个问题.fixedsize()

GeometryReader{ reader in
        ScrollView{
            Text(reader.size.height.description)
            Text(reader.size.height.description)

                    FlowStack(columns: 3, numItems: 27, alignment: .leading) { index, colWidth in
                       if index == 0{
                           Text(" \(index) ").frame(width: colWidth).border(Color.gray)
                            .background(Color.red)

                       }
                       else if index == 1{
                           Text(" \(index) ").frame(width: colWidth).border(Color.gray)

                       }
                       else if index == 2{
                           Text(" \(index) ").frame(width: colWidth).border(Color.gray)


                       }
                       else{
                        Text(" \(index) ").frame(width: colWidth).border(Color.gray)


                       }
                    }
                    .background(Color.red)
            Text(reader.size.height.description)
            Text(reader.size.height.description)



        }.fixedSize(horizontal: false, vertical: false)

    }

结果

我想要的是

4

1 回答 1

1

我不知道您在FlowStackstruct 中写了什么,这就是为什么很难给出正确答案的原因。我是如何解决这个网格的:

struct GridScrollView: View {

    var body: some View {

        GeometryReader{ geometry in

            VStack {

                Text("height: \(geometry.size.height.description)")
                Text("width: \(geometry.size.width.description)")

                ScrollView{
                    FlowStack(columns: 3, numItems: 60, width: geometry.size.width, height: geometry.size.height)
                }
            }
        }

    }
}

我的FlowStack代码:

struct FlowStack: View {

    var columns: Int
    var numItems: Int
    var width: CGFloat
    var height: CGFloat

    private var numberOfRows: Int {
        get {
            numItems / columns
        }
    }

    private var cellWidth: CGFloat {
        get {
            width / CGFloat(columns) - 4 // with padding
        }
    }

    private var cellHeight: CGFloat {
        get {
            height / CGFloat(numberOfRows) - 4 // with padding
        }
    }

    var body: some View {

        ForEach(0..<self.numberOfRows, id: \.self) { row in

            HStack {
                ForEach(1...self.columns, id: \.self) { col in
                    Text("\(row * self.columns + col)")
                        .frame(width: self.cellWidth, height: self.cellHeight, alignment: .center)
                        .background(Color.red)
                        .border(Color.gray)
                    .padding(2)
                }
            }

        }

    }

}

结果是:

在此处输入图像描述

PS这是我的快速解决方案。我认为您也可以使用来自 github的这个网格,它非常庞大,但在我看来很灵活

于 2019-11-14T04:25:56.457 回答