---
title: AnyLayout
framework: swiftui
role: symbol
role_heading: Structure
path: swiftui/anylayout
---

# AnyLayout

A type-erased instance of the layout protocol.

## Declaration

```swift
@frozen struct AnyLayout
```

## Overview

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 protocol. The above example chooses between the HStackLayout and VStackLayout types, which are versions of the built-in HStack and VStack containers that conform to the protocol. You can also use custom layout types that you define.

## Topics

### Creating the layout

- [init(_:)](swiftui/anylayout/init(_:).md)

## Relationships

### Conforms To

- [Animatable](swiftui/animatable.md)
- [Layout](swiftui/layout.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Transitioning between layout types

- [HStackLayout](swiftui/hstacklayout.md)
- [VStackLayout](swiftui/vstacklayout.md)
- [ZStackLayout](swiftui/zstacklayout.md)
- [GridLayout](swiftui/gridlayout.md)
