【问题标题】:Nested ScrollView SwiftUI嵌套的 ScrollView SwiftUI
【发布时间】:2019-11-13 18:55:41
【问题描述】:

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

【问题讨论】:

  • 只有这一段代码在你之后有点难以重复,你能至少给FlowStack struct吗?

标签: scrollview swiftui ios13 swift5 swiftui-list


【解决方案1】:

我不知道你在FlowStack struct 中写了什么,这就是为什么很难给出正确答案的原因。这是我解决这个网格的方法:

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

        }

    }

}

结果是:

P.S. 这是我的快速解决方案。我认为您也可以使用this grid from github,它非常庞大,但在我看来很灵活

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多