0

I have a LazyVGrid that contains 5 columns. I have set the maxWidth to the geometry width which seems to work, however the LazyVGrid doesn't seem to respect this width and is cropping subviews. Is there any way around this?

This code:

struct ContentView: View {
    let data = (1...20).map { "Item \($0)" }

    func columns(_ count: Int, totalWidth: CGFloat) -> [GridItem] {
        var columns: [GridItem] = []
        for _ in 1...count {
            columns.append(GridItem(.fixed(totalWidth/CGFloat(count))))
            print("GEOMETRY WIDTH: \(totalWidth)")
            print("UIScreen WIDTH: \(UIScreen.main.bounds.width)")
        }
        return columns
    }

    var body: some View {
        GeometryReader { geometry in
            ScrollView {
                ForEach(data, id: \.self) { item in
                    LazyHStack(alignment: .center, spacing: .none, pinnedViews: .sectionHeaders) {
                        LazyVGrid(columns: columns((1...10).randomElement() ?? 1, totalWidth: geometry.size.width)) {
                            ForEach(data, id: \.self) { item in
                                Text(item)
                            }
                        }
                        .frame(width: geometry.size.width)
                    }
                    .frame(width: geometry.size.width)
                }
            }
        }
    }
}

Produces this result

Code Output

Ideally, i'd like the views to overflow to the next row if they don't fit.

Brandon Stillitano
  • 1,304
  • 8
  • 27
  • It is not really clear what do you want to achieve. Why LazyVGrid inside LazyHStack? HStack grows horizontally and you have fixed width, so yes something goes out of screen in such construction. It seems to me LazyHStack is redundant here, or you probably expected from it not really that it does. – Asperi May 11 '22 at 03:06
  • @Asperi thanks for your input. I am trying to achieve a layout where I can pass in a number of columns and have the views within each column rendered and stacked vertically – Brandon Stillitano May 11 '22 at 03:10
  • Take a look at this.. https://stackoverflow.com/a/63397243/14414215 I had to rewrite my view from using lazyVGrid to using Hstacks w/ help from that SO. Somehow I was getting 100% CPU and app hang when using lazyVGrid – app4g Jan 28 '23 at 15:32

0 Answers0