swift-snapshot-testing icon indicating copy to clipboard operation
swift-snapshot-testing copied to clipboard

Is there any possible way to snapshot a screen with modal sheet on the screen

Open looneytsci opened this issue 1 year ago • 3 comments

I'm trying to make a snapshot for a view, which can open bottomSheet via .fullScreenCover and transparent background. Looks like It captures only the view layer without bottomSheet layer. Can I make the snapshot of the full view hierarchy? Or can I implement some custom snapshotting strategy with modal screen capturing?

looneytsci avatar May 09 '23 10:05 looneytsci

PS this is for SwiftUI project

looneytsci avatar May 09 '23 10:05 looneytsci

I can make a snapshot of a Alert using this code below, propably it'll work to sheets @looneytsci

extension Snapshotting where Value: UIViewController, Format == UIImage {    
    public static func windowedImage(interfaceStyle: UIUserInterfaceStyle = .unspecified,
                                     precision: Float = 0.98,
                                     perceptualPrecision: Float = 1) -> Snapshotting {
        return Snapshotting<UIImage, UIImage>.image(
            precision: precision,
            perceptualPrecision: perceptualPrecision,
            scale: nil
        ).asyncPullback { viewController in
            Async<UIImage> { callback in
                UIView.setAnimationsEnabled(false)
                let window = UIApplication.shared.windows.first!
                window.rootViewController = viewController
                window.windowScene?.traitOverrides.userInterfaceStyle = interfaceStyle
                DispatchQueue.main.async {
                    let image = UIGraphicsImageRenderer(bounds: window.bounds).image { _ in
                        window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
                    }
                    callback(image)
                    UIView.setAnimationsEnabled(true)
                }
            }
        }
    }
}

tilucasoli avatar Nov 07 '23 20:11 tilucasoli

Im facing the same issue. The code above doesn't snapshot a sheet.

LostStudent avatar Mar 20 '24 15:03 LostStudent