Created
June 8, 2022 20:05
-
-
Save Sherlouk/f3956b440333084ef9ea1e505856500c to your computer and use it in GitHub Desktop.
SwiftUI view for adding accessibility previews. Proof of concept.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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