Skip to content

Instantly share code, notes, and snippets.

@Sherlouk
Created June 8, 2022 20:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Sherlouk/f3956b440333084ef9ea1e505856500c to your computer and use it in GitHub Desktop.
Save Sherlouk/f3956b440333084ef9ea1e505856500c 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