Tengo un LazyHGrid que muestra varias vistas de texto en una fila. Ese LazyHGrid está dentro de un VStack. Sin embargo, el LazyHGrid es más alto de lo que debe ser. ¿Qué causa el espacio extra?
struct ContentView: View { let rows = [GridItem()] var body: some View { VStack { ScrollView(.horizontal) { LazyHGrid(rows: rows) { ForEach(0..<10) { index in Text("Test \(index)") .padding() .foregroundColor(Color.white) .background(Color.black) } } } .background(Color.green) ScrollView(.vertical) { VStack { ForEach(0..<100) { index in Text(String(index)) .frame(maxWidth: .infinity) } } } .background(Color.blue) } } }Esto funciona: leyendo la altura de las celdas con una superposición con GeometryReader
struct ContentView: View { let rows = [ GridItem() ] @State private var contentSize = CGFloat.zero var body: some View { VStack { ScrollView(.horizontal) { LazyHGrid(rows: rows) { ForEach(0..<10) { index in Text("Test \(index)") .padding() .foregroundColor(.white) .background(.black) .overlay( GeometryReader { geo in Color.clear.onAppear { contentSize = geo.size.height } } ) } } } .frame(height: contentSize + 20) .background(Color.green) ScrollView(.vertical) { VStack { ForEach(0..<100) { index in Text(String(index)) .frame(maxWidth: .infinity) } } } .background(Color.blue) } } }Solo tiene que agregar el modificador .fixedSize() a su cuadrícula, y funciona...
LazyHGrid(rows: rows) { ForEach(0..<10) { index in Text("Test \(index)") .padding() .foregroundColor(Color.white) .background(Color.black) } } .fixedSize()