AnyLayout
A type-erased instance of the layout protocol.
Declaration
@frozen struct AnyLayoutOverview
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.