From 6bb1cef1bae09b9b2bec06ae808e71db16d2ecde Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 7 Nov 2026 16:01:26 +0800 Subject: [PATCH] fix: keep visionOS hosting backgrounds transparent Fixes #1251 --- .../Hosting/UIKit/View/UIHostingView.swift | 6 ++++++ ...IHostingControllerCompatibilityTests.swift | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 Tests/OpenSwiftUICompatibilityTests/Integration/Hosting/UIKit/UIHostingControllerCompatibilityTests.swift diff --git a/Sources/OpenSwiftUI/Integration/Hosting/UIKit/View/UIHostingView.swift b/Sources/OpenSwiftUI/Integration/Hosting/UIKit/View/UIHostingView.swift index 6a1e680a6..cc03a5395 100644 --- a/Sources/OpenSwiftUI/Integration/Hosting/UIKit/View/UIHostingView.swift +++ b/Sources/OpenSwiftUI/Integration/Hosting/UIKit/View/UIHostingView.swift @@ -386,6 +386,12 @@ open class _UIHostingView: UIView, XcodeViewDebugDataProvider where Con if wantsTransparentBackground { super.backgroundColor = nil } else { + #if os(visionOS) + if traitCollection.userInterfaceIdiom == .vision { + super.backgroundColor = nil + return + } + #endif setBackground(.systemBackground) } } diff --git a/Tests/OpenSwiftUICompatibilityTests/Integration/Hosting/UIKit/UIHostingControllerCompatibilityTests.swift b/Tests/OpenSwiftUICompatibilityTests/Integration/Hosting/UIKit/UIHostingControllerCompatibilityTests.swift new file mode 100644 index 000000000..235871cb2 --- /dev/null +++ b/Tests/OpenSwiftUICompatibilityTests/Integration/Hosting/UIKit/UIHostingControllerCompatibilityTests.swift @@ -0,0 +1,21 @@ +// +// UIHostingControllerCompatibilityTests.swift +// OpenSwiftUICompatibilityTests + +#if os(visionOS) +import Testing +import UIKit + +@MainActor +struct UIHostingControllerCompatibilityTests { + @Test(arguments: [UIUserInterfaceStyle.light, .dark]) + func visionBackgroundIsTransparent(style: UIUserInterfaceStyle) { + let controller = UIHostingController(rootView: EmptyView()) + controller.overrideUserInterfaceStyle = style + controller.loadViewIfNeeded() + + #expect(controller.view.traitCollection.userInterfaceIdiom == .vision) + #expect(controller.view.backgroundColor == nil) + } +} +#endif