Skip to content

Instantly share code, notes, and snippets.

@minsOne
Forked from Sherlouk/AccessibilityPreview.swift
Created October 18, 2022 10:31
Show Gist options
  • Save minsOne/f4d6b34c1cad945a2d01020be15d744f to your computer and use it in GitHub Desktop.
Save minsOne/f4d6b34c1cad945a2d01020be15d744f to your computer and use it in GitHub Desktop.
SwiftUI view for adding accessibility previews. Proof of concept.
import SwiftUI
import UIKit
// from https://github.com/cashapp/AccessibilitySnapshot
import AccessibilitySnapshotCore
struct AccessibilityPreview<Content: View>: View {
let content: Content
var body: some View {
AccessibilityPreviewRepresentable(content: content)
.previewLayout(.fixed(
width: UIScreen.main.bounds.width * 2,
height: UIScreen.main.bounds.height
))
.previewDisplayName("VoiceOver Representation")
}
}
private struct AccessibilityPreviewRepresentable<Content: View>: UIViewRepresentable {
let content: Content
func makeUIView(context: Context) -> some UIView {
let view = UIHostingController(rootView: content)
view.view.frame = UIScreen.main.bounds
let snapshotView = AccessibilitySnapshotView(
containedView: view.view,
viewRenderingMode: .drawHierarchyInRect,
activationPointDisplayMode: .whenOverridden
)
// who needs error management 'eh?
try! snapshotView.parseAccessibility(useMonochromeSnapshot: false)
return snapshotView
}
func updateUIView(_ uiView: UIViewType, context: Context) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment