Skip to content

Instantly share code, notes, and snippets.

@joeltrew
Created May 17, 2022 11:48
Show Gist options
  • Save joeltrew/dadb512312052d8070d59170b2bf586f to your computer and use it in GitHub Desktop.
Save joeltrew/dadb512312052d8070d59170b2bf586f to your computer and use it in GitHub Desktop.
AccessibilityScrollModifier lets you place content in a scrollview when dynamic text hits certain sizes or by default an Accessibility Category size
struct AccessibilityScrollModifier: ViewModifier {
@Environment(\.sizeCategory) private var sizeCategory
var axis: Axis.Set
var shouldScroll: (ContentSizeCategory) -> Bool
func body(content: Content) -> some View {
if shouldScroll(sizeCategory) {
ScrollView(axis, showsIndicators: true) {
content
}
} else {
content
}
}
}
extension View {
func accessibilityScrollView(axis: Axis.Set = .vertical) -> some View {
self.modifier(AccessibilityScrollModifier(axis: axis, shouldScroll: { $0.isAccessibilityCategory }))
}
func accessibilityScrollView(axis: Axis.Set = .vertical, activateAt sizeCategory: ContentSizeCategory) -> some View {
self.modifier(AccessibilityScrollModifier(axis: axis, shouldScroll: { $0 >= sizeCategory }))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment