LazyVGrid
A container view that arranges its child views in a grid that grows vertically, creating items only as needed.
Declaration
import { LazyVGrid } from 'swiftui-react-native';nonisolated struct LazyVGrid<Content> where Content : ViewInitializers
One SwiftUI initializer backs <LazyVGrid>.
<LazyVGrid
columns: SwiftUI.GridItem[]
alignment?: SwiftUI.HorizontalAlignment
spacing?: number | null
pinnedViews?: SwiftUI.PinnedScrollableViews
>
children?: ReactNode
</LazyVGrid>nonisolated init(
columns: [GridItem],
alignment: HorizontalAlignment = .center,
spacing: CGFloat? = nil,
pinnedViews: PinnedScrollableViews = .init(),
@ContentBuilder content: () -> Content
)columns*SwiftUI.GridItem[]An array of grid items to size and position each row of the grid.
alignmentSwiftUI.HorizontalAlignmentThe alignment of the grid within its parent view.
Default
`.center`spacingnumber | nullThe spacing between the grid and the next item in its parent view.
Default
`nil`pinnedViewsSwiftUI.PinnedScrollableViewsViews to pin to the bounds of a parent scroll view.
Default
`.init()`childrenReactNodeContent. Strings become
Text; React Native views are laid out by Yoga and placed by SwiftUI.Swift
content: () -> Content
Also takes the common view props: modifiers, style and testID.
Overview
Use a lazy vertical grid when you want to display a large, vertically
scrollable collection of views arranged in a two dimensional layout. The
first view that you provide to the grid's content closure appears in the
top row of the column that's on the grid's leading edge. Additional views
occupy successive cells in the grid, filling the first row from leading to
trailing edges, then the second row, and so on. The number of rows can grow
unbounded, but you specify the number of columns by providing a
corresponding number of GridItem instances to the grid's initializer.
The grid in the following example defines two columns and uses a
ForEach structure to repeatedly generate a pair of Text views for
the columns in each row:
struct VerticalSmileys: View {
let columns = [GridItem(.flexible()), GridItem(.flexible())]
var body: some View {
ScrollView {
LazyVGrid(columns: columns) {
ForEach(0x1f600...0x1f679, id: \.self) { value in
Text(String(format: "%x", value))
Text(emoji(value))
.font(.largeTitle)
}
}
}
}
private func emoji(_ value: Int) -> String {
guard let scalar = UnicodeScalar(value) else { return "?" }
return String(Character(scalar))
}
}For each row in the grid, the first column shows a Unicode code point from the "Smileys" group, and the second shows its corresponding emoji:
You can achieve a similar layout using a Grid container. Unlike a lazy
grid, which creates child views only when SwiftUI needs to display
them, a regular grid creates all of its child views right away. This
enables the grid to provide better support for cell spacing and alignment.
Only use a lazy grid if profiling your app shows that a Grid view
performs poorly because it tries to load too many views at once.
From Apple’s SwiftUI documentation. Code samples there are Swift.
Relationships
Conforms to
- View
See also
More in Layout.
- AnyLayoutA type-erased instance of the layout protocol.
- GeometryReaderA container view that defines its content as a function of its own size and coordinate space.
- GlassEffectContainerA view that combines multiple glass shapes into a single shape that can morph individual shapes into one another.
- GridA container view that arranges other views in a two dimensional layout.
- GridLayoutA grid that you can use in conditional layouts.
- GridRowA horizontal row in a two dimensional grid container.
- HStackA view that arranges its subviews in a horizontal line.
- HStackLayoutA horizontal container that you can use in conditional layouts.
- LazyHGridA container view that arranges its child views in a grid that grows horizontally, creating items only as needed.
- LazyHStackA view that arranges its children in a line that grows horizontally, creating items only as needed.
- LazyVStackA view that arranges its children in a line that grows vertically, creating items only as needed.
- ScrollViewA scrollable view.