<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/Grid/init(alignment:horizontalSpacing:verticalSpacing:content:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4GridV9alignment17horizontalSpacing08verticalF07contentACyxGAA9AlignmentV_14CoreFoundation7CGFloatVSgANxyXEtcfc"
  },
  "title" : "init(alignment:horizontalSpacing:verticalSpacing:content:)"
}
-->

# init(alignment:horizontalSpacing:verticalSpacing:content:)

Creates a grid with the specified spacing, alignment, and child
views.

```
nonisolated init(alignment: Alignment = .center, horizontalSpacing: CGFloat? = nil, verticalSpacing: CGFloat? = nil, @ContentBuilder content: () -> Content)
```

## Parameters

`alignment`

The guide for aligning the child views within the
space allocated for a given cell. The default is
[`center`](/documentation/SwiftUI/Alignment/center).

`horizontalSpacing`

The horizontal distance between each cell, given
in points. The value is `nil` by default, which results in a
default distance between cells that’s appropriate for the platform.

`verticalSpacing`

The vertical distance between each cell, given
in points. The value is `nil` by default, which results in a
default distance between cells that’s appropriate for the platform.

`content`

A closure that creates the grid’s rows.

## Discussion

Use this initializer to create a [`Grid`](/documentation/SwiftUI/Grid). Provide a content closure
that defines the rows of the grid, and optionally customize the
spacing between cells and the alignment of content within each cell.
The following example customizes the spacing between cells:

```
Grid(horizontalSpacing: 30, verticalSpacing: 30) {
    ForEach(0..<5) { row in
        GridRow {
            ForEach(0..<5) { column in
                Text("(\(column), \(row))")
            }
        }
    }
}
```

You can list rows and the cells within rows directly, or you can
use a [`ForEach`](/documentation/SwiftUI/ForEach) structure to generate either, as the example above
does:

![A screenshot of a grid that contains five rows and five columns.](images/com.apple.SwiftUI/Grid-init-1-iOS@2x.png)

By default, the grid’s alignment value applies to all of the cells in
the grid. However, you can also change the alignment for particular
cells or groups of cells:

- Override the vertical alignment for the cells in a row
  by specifying a [`VerticalAlignment`](/documentation/SwiftUI/VerticalAlignment) parameter to the corresponding
  row’s [`init(alignment:content:)`](/documentation/SwiftUI/GridRow/init(alignment:content:)) initializer.
- Override the horizontal alignment for the cells in a column by adding
  a [`gridColumnAlignment(_:)`](/documentation/SwiftUI/View/gridColumnAlignment(_:)) view modifier to exactly one of
  the cells in the column, and specifying a [`HorizontalAlignment`](/documentation/SwiftUI/HorizontalAlignment)
  parameter.
- Specify a custom alignment anchor for a particular cell by using the
  [`gridCellAnchor(_:)`](/documentation/SwiftUI/View/gridCellAnchor(_:)) modifier on the cell’s view.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)