Altern
Altern

Reputation: 31

Nested ScrollView SwiftUI

I am trying to put grid(developed with scrollview) inside scrollview but I can't set grid height with content height. I can't handle this problem with .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

Upvotes: 0

Views: 2167

Answers (1)

Hrabovskyi Oleksandr
Hrabovskyi Oleksandr

Reputation: 3275

I don't know what did you wrote in FlowStack struct, that's why it's really hard to give right answer. There is how I solve this grid:

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

    }
}

my FlowStack code:

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

        }

    }

}

and the result is:

enter image description here

P.S. that's my quick solution. I think you can also use this grid from github, it's quite massive, but flexible in my opinion

Upvotes: 1

Related Questions