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

# AnyLayout

A type-erased instance of the layout protocol.

```
@frozen struct AnyLayout
```

## Overview

Use an `AnyLayout` instance to enable dynamically changing the
type of a layout container without destroying the state of the subviews.
For example, you can create a layout that changes between horizontal and
vertical layouts based on the current Dynamic Type setting:

```
struct DynamicLayoutExample: View {
    @Environment(\.dynamicTypeSize) var dynamicTypeSize

    var body: some View {
        let layout = dynamicTypeSize <= .medium ?
            AnyLayout(HStackLayout()) : AnyLayout(VStackLayout())

        layout {
            Text("First label")
            Text("Second label")
        }
    }
}
```

The types that you use with `AnyLayout` must conform to the [`Layout`](/documentation/SwiftUI/Layout)
protocol. The above example chooses between the [`HStackLayout`](/documentation/SwiftUI/HStackLayout) and
[`VStackLayout`](/documentation/SwiftUI/VStackLayout) types, which are versions of the built-in [`HStack`](/documentation/SwiftUI/HStack)
and [`VStack`](/documentation/SwiftUI/VStack) containers that conform to the protocol. You can also
use custom layout types that you define.

## Topics

### Creating the layout

[`init(_:)`](/documentation/SwiftUI/AnyLayout/init(_:))

Creates a type-erased value that wraps the specified layout.



---

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)