Skip to content

Instantly share code, notes, and snippets.

@robinkanatzar
Last active August 4, 2024 07:37
Show Gist options
  • Save robinkanatzar/6b281658e7bf4cdde73d69f86a4c8a8e to your computer and use it in GitHub Desktop.
Save robinkanatzar/6b281658e7bf4cdde73d69f86a4c8a8e to your computer and use it in GitHub Desktop.
AStack: An accessible stack view that switches from an HStack to a VStack when the dynamic type size is an accessibility size
/// An accessible stack view that starts as an HStack
/// then becomes a VStack once the dynamic type size
/// is an accessibility size.
///
/// Usage in a View:
/// ```
/// var body: some View {
/// AStack {
/// Text("Hello, world!")
/// Text("Hello, world!")
/// Text("Hello, world!")
/// Text("Hello, world!")
/// Text("Hello, world!")
/// Text("Hello, world!")
/// Text("Hello, world!")
/// }
/// }
/// ```
struct AStack<Content>: View where Content: View {
@Environment(\.dynamicTypeSize) var dynamicTypeSize
let content: Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
var body: some View {
if dynamicTypeSize.isAccessibilitySize {
VStack { content }
} else {
HStack { content }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment