0

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

1
  • It's a little hard to repeat after you with only this piece of code, can you give at least FlowStack struct? Commented Nov 14, 2019 at 3:29

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.