Skip to content

Added an isOpaque parameter to view strategies to allow for transparent snapshots #776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/SnapshotTesting/Common/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@
func snapshotView(
config: ViewImageConfig,
drawHierarchyInKeyWindow: Bool,
isOpaque: Bool,
traits: UITraitCollection,
view: UIView,
viewController: UIViewController
Expand All @@ -987,6 +988,11 @@
addImagesForRenderedViews(view).sequence().run { views in
callback(
renderer(bounds: view.bounds, for: traits).image { ctx in
if isOpaque == false {
UIColor.clear.setFill()
ctx.fill(view.bounds)
}

if drawHierarchyInKeyWindow {
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
} else {
Expand Down
5 changes: 5 additions & 0 deletions Sources/SnapshotTesting/Snapshotting/SwiftUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
/// - traits: A trait collection override.
public static func image(
drawHierarchyInKeyWindow: Bool = false,
isOpaque: Bool = true,
precision: Float = 1,
perceptualPrecision: Float = 1,
layout: SwiftUISnapshotLayout = .sizeThatFits,
Expand Down Expand Up @@ -72,6 +73,9 @@
)
} else {
let hostingController = UIHostingController.init(rootView: view)
if isOpaque == false {
hostingController.view.backgroundColor = .clear
}

let maxSize = CGSize(width: 0.0, height: 0.0)
config.size = hostingController.sizeThatFits(in: maxSize)
Expand All @@ -82,6 +86,7 @@
return snapshotView(
config: config,
drawHierarchyInKeyWindow: drawHierarchyInKeyWindow,
isOpaque: isOpaque,
traits: traits,
view: controller.view,
viewController: controller
Expand Down
2 changes: 2 additions & 0 deletions Sources/SnapshotTesting/Snapshotting/UIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/// - traits: A trait collection override.
public static func image(
drawHierarchyInKeyWindow: Bool = false,
isOpaque: Bool = true,
precision: Float = 1,
perceptualPrecision: Float = 1,
size: CGSize? = nil,
Expand All @@ -36,6 +37,7 @@
snapshotView(
config: .init(safeArea: .zero, size: size ?? view.frame.size, traits: .init()),
drawHierarchyInKeyWindow: drawHierarchyInKeyWindow,
isOpaque: isOpaque,
traits: traits,
view: view,
viewController: .init()
Expand Down
4 changes: 4 additions & 0 deletions Sources/SnapshotTesting/Snapshotting/UIViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
/// - traits: A trait collection override.
public static func image(
on config: ViewImageConfig,
isOpaque: Bool = true,
precision: Float = 1,
perceptualPrecision: Float = 1,
size: CGSize? = nil,
Expand All @@ -35,6 +36,7 @@
config: size.map { .init(safeArea: config.safeArea, size: $0, traits: config.traits) }
?? config,
drawHierarchyInKeyWindow: false,
isOpaque: isOpaque,
traits: traits,
view: viewController.view,
viewController: viewController
Expand All @@ -57,6 +59,7 @@
/// - traits: A trait collection override.
public static func image(
drawHierarchyInKeyWindow: Bool = false,
isOpaque: Bool = true,
precision: Float = 1,
perceptualPrecision: Float = 1,
size: CGSize? = nil,
Expand All @@ -71,6 +74,7 @@
snapshotView(
config: .init(safeArea: .zero, size: size, traits: traits),
drawHierarchyInKeyWindow: drawHierarchyInKeyWindow,
isOpaque: isOpaque,
traits: traits,
view: viewController.view,
viewController: viewController
Expand Down