嵌套ScrollView SwiftUI

我正在尝试将网格(使用scrollview开发的)放在scrollview内,但是无法设置内容高度的网格高度。我无法用.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)

    }

Result

What I want

oOFoXOo 回答:嵌套ScrollView SwiftUI

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

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

        }

    }

}

结果是:

enter image description here

附言是我的快速解决方案。我认为您也可以使用this grid from github,它很大,但我认为它很灵活

本文链接:https://www.f2er.com/3107474.html

大家都在问