<!--
{
  "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/ViewSpacing",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI11ViewSpacingV"
  },
  "title" : "ViewSpacing"
}
-->

# ViewSpacing

A collection of the geometric spacing preferences of a view.

```
struct ViewSpacing
```

## Overview

This type represents how much space a view prefers to have between it and
the next view in a layout. The type stores independent values
for each of the top, bottom, leading, and trailing edges,
and can also record different values for different kinds of adjacent
views. For example, it might contain one value for the spacing to the next
text view along the top and bottom edges, other values for the spacing to
text views on other edges, and yet other values for other kinds of views.
Spacing preferences can also vary by platform.

Your [`Layout`](/documentation/SwiftUI/Layout) type doesn’t have to take preferred spacing into
account, but if it does, you can use the [`spacing`](/documentation/SwiftUI/LayoutSubview/spacing)
preferences of the subviews in your layout container to:

- Add space between subviews when you implement the
  [`placeSubviews(in:proposal:subviews:cache:)`](/documentation/SwiftUI/Layout/placeSubviews(in:proposal:subviews:cache:)) method.
- Create a spacing preferences instance for the container view by
  implementing the [`spacing(subviews:cache:)`](/documentation/SwiftUI/Layout/spacing(subviews:cache:)) method.

## Topics

### Creating spacing instances

[`init()`](/documentation/SwiftUI/ViewSpacing/init())

Initializes an instance with default spacing values.

[`static let zero: ViewSpacing`](/documentation/SwiftUI/ViewSpacing/zero)

A view spacing instance that contains zero on all edges.

### Measuring spacing distance

[`func distance(to: ViewSpacing, along: Axis) -> CGFloat`](/documentation/SwiftUI/ViewSpacing/distance(to:along:))

Gets the preferred spacing distance along the specified axis to the view
that returns a specified spacing preference.

### Merging spacing instances

[`func formUnion(ViewSpacing, edges: Edge.Set)`](/documentation/SwiftUI/ViewSpacing/formUnion(_:edges:))

Merges the spacing preferences of another spacing instance with this
instance for a specified set of edges.

[`func union(ViewSpacing, edges: Edge.Set) -> ViewSpacing`](/documentation/SwiftUI/ViewSpacing/union(_:edges:))

Gets a new value that merges the spacing preferences of another spacing
instance with this instance for a specified set of edges.

## Relationships

### Conforms To

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Sendable`](/documentation/Swift/Sendable)

---

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)