One Logo Pool Ball
SwiftUI · IOS API REFERENCE

GridItem

struct · iOS 14.0+ · SDK 27.1

struct GridItem

A description of a row or a column in a lazy grid. Use an array of `GridItem` instances to configure the layout of items in a lazy grid. Each grid item in the array specifies layout properties like size and spacing for the rows of a ``LazyHGrid`` or the columns of a ``LazyVGrid``. The following example defines four rows for a horizontal grid, each with different characteristics: struct GridItemDemo: View { let rows = [ GridItem(.fixed(30), spacing: 1), GridItem(.fixed(60), spacing: 10), GridItem(.fixed(90), spacing: 20), GridItem(.fixed(10), spacing: 50) ] var body: some View { ScrollView(.horizontal) { LazyHGrid(rows: rows, spacing: 5) { ForEach(0...300, id: \.self) { _ in Color.red.frame(width: 30) Color.green.frame(width: 30) Color.blue.frame(width: 30) Color.yellow.frame(width: 30) } } } } } A lazy horizontal grid sets the width of each column based on the widest cell in the column. It can do this because it has access to all of the views in a given column at once. In the example above, the ``Color`` views always have the same fixed width, resulting in a uniform column width across the whole grid. However, a lazy horizontal grid doesn't generally have access to all the views in a row, because it generates new cells as people scroll through information in your app. Instead, it relies on a grid item for information about each row. The example above indicates a different fixed height for each row, and sets a different amount of spacing to appear after each row: ![A screenshot of a grid of rectangles arranged in four rows and a large number of colums. All the rectangles are the same width, and have a uniform horizontal spacing. The rectangles in a given row are the same height as each other, but different than the rectangles in other rows. The vertical spacing between rows also varies.](GridItem-1-iOS)

Members (7)

get · iOS 14.0+

size

TypeScript
get size(): GridItem_Size
Swift declaration
var size: GridItem.Size

The size of the item, which is the width of a column item or the height of a row item.

set · iOS 14.0+

size

TypeScript
set size(value: GridItem_Size)
Swift declaration
var size: GridItem.Size

The size of the item, which is the width of a column item or the height of a row item.

get · iOS 14.0+

spacing

TypeScript
get spacing(): (number) | null
Swift declaration
var spacing: CGFloat?

The spacing to the next item. If this value is `nil`, the item uses a reasonable default for the current platform.

set · iOS 14.0+

spacing

TypeScript
set spacing(value: (number) | null)
Swift declaration
var spacing: CGFloat?

The spacing to the next item. If this value is `nil`, the item uses a reasonable default for the current platform.

get · iOS 14.0+

alignment

TypeScript
get alignment(): (_SwiftUICore_Alignment) | null
Swift declaration
var alignment: Alignment?

The alignment to use when placing each view. Use this property to anchor the view's relative position to the same relative position in the view's assigned grid space.

set · iOS 14.0+

alignment

TypeScript
set alignment(value: (_SwiftUICore_Alignment) | null)
Swift declaration
var alignment: Alignment?

The alignment to use when placing each view. Use this property to anchor the view's relative position to the same relative position in the view's assigned grid space.

init · iOS 14.0+

init

TypeScript
static init(size?: GridItem_Size, spacing?: (number) | null, alignment?: (_SwiftUICore_Alignment) | null): GridItem
Swift declaration
init(_ size: GridItem.Size = .flexible(), spacing: CGFloat? = nil, alignment: Alignment? = nil)

Creates a grid item with the specified size, spacing, and alignment. - Parameters: - size: The size of the grid item. - spacing: The spacing to use between this and the next item. - alignment: The alignment to use for this grid item.

Skipped declarations

No declarations were skipped in this snapshot.

All SwiftUI types →