diff --git a/Sources/SnapshotTesting/Common/View.swift b/Sources/SnapshotTesting/Common/View.swift index fb6261c94..cc402109b 100644 --- a/Sources/SnapshotTesting/Common/View.swift +++ b/Sources/SnapshotTesting/Common/View.swift @@ -960,8 +960,11 @@ func snapshotView( view: view, viewController: viewController ) + // NB: Avoid safe area influence. + let directionalLayoutMargins = view.directionalLayoutMargins if config.safeArea == .zero { view.frame.origin = .init(x: offscreen, y: offscreen) } + view.directionalLayoutMargins = directionalLayoutMargins return (view.snapshot ?? Async { callback in addImagesForRenderedViews(view).sequence().run { views in diff --git a/Tests/SnapshotTestingTests/SnapshotTestingTests.swift b/Tests/SnapshotTestingTests/SnapshotTestingTests.swift index 1de4623a6..72dd7a090 100644 --- a/Tests/SnapshotTestingTests/SnapshotTestingTests.swift +++ b/Tests/SnapshotTestingTests/SnapshotTestingTests.swift @@ -854,6 +854,35 @@ final class SnapshotTestingTests: XCTestCase { #endif } + func testUIViewControllerPreserveDirectionalLayoutMargins() { + #if os(iOS) + class ViewController: UIViewController { + + let blueView: UIView = { + let view = UIView() + view.backgroundColor = .blue + view.translatesAutoresizingMaskIntoConstraints = false + return view + }() + + override func viewDidLoad() { + super.viewDidLoad() + view.backgroundColor = .white + view.addSubview(blueView) + NSLayoutConstraint.activate([ + blueView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor), + blueView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor), + blueView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor), + blueView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor) + ]) + } + } + let device: ViewImageConfig = .iPhone8(.landscape) + let vc = ViewController() + assertSnapshot(matching: vc, as: .image(on: device)) + #endif + } + func testUIViewControllerLifeCycle() { #if os(iOS) class ViewController: UIViewController { diff --git a/Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testUIViewControllerPreserveDirectionalLayoutMargins.1.png b/Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testUIViewControllerPreserveDirectionalLayoutMargins.1.png new file mode 100644 index 000000000..89c5feea2 Binary files /dev/null and b/Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testUIViewControllerPreserveDirectionalLayoutMargins.1.png differ