<!--
{
  "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/Layout/spacing(subviews:cache:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI6LayoutP7spacing8subviews5cacheAA11ViewSpacingVAA0C8SubviewsV_5CacheQzztF"
  },
  "title" : "spacing(subviews:cache:)"
}
-->

# spacing(subviews:cache:)

Returns the preferred spacing values of the composite view.

```
func spacing(subviews: Self.Subviews, cache: inout Self.Cache) -> ViewSpacing
```

## Parameters

`subviews`

A collection of proxy instances that represent the
views that the container arranges. You can use the proxies in the
collection to get information about the subviews as you determine
how much spacing the container prefers around it.

`cache`

Optional storage for calculated data that you can share among
the methods of your custom layout container. See
[`makeCache(subviews:)`](/documentation/SwiftUI/Layout/makeCache(subviews:)) for details.

## Return Value

A [`ViewSpacing`](/documentation/SwiftUI/ViewSpacing) instance that describes the preferred
spacing around the container view.

## Discussion

Implement this method to provide custom spacing preferences
for a layout container. The value you return affects
the spacing around the container, but it doesn’t affect how the
container arranges subviews relative to one another inside the
container.

Create a custom [`ViewSpacing`](/documentation/SwiftUI/ViewSpacing) instance for your container by
initializing one with default values, and then merging that with
spacing instances of certain subviews. For example, if you define
a basic vertical stack that places subviews in a column, you could
use the spacing preferences of the subview edges that make
contact with the container’s edges:

```
extension BasicVStack {
    func spacing(subviews: Subviews, cache: inout ()) -> ViewSpacing {
        var spacing = ViewSpacing()

        for index in subviews.indices {
            var edges: Edge.Set = [.leading, .trailing]
            if index == 0 { edges.formUnion(.top) }
            if index == subviews.count - 1 { edges.formUnion(.bottom) }
            spacing.formUnion(subviews[index].spacing, edges: edges)
        }

        return spacing
    }
}
```

In the above example, the first and last subviews contribute to the
spacing above and below the container, respectively, while all subviews
affect the spacing on the leading and trailing edges.

If you don’t implement this method, the protocol provides a default
implementation that merges the spacing preferences across all subviews on all edges.

---

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)