如何将 UIHostingController 的背景颜色设置为清除?

Gar*_*abo 2 ios swift swiftui uihostingcontroller zstack

我需要能够查看我的 SwiftUI 视图以及呈现它们的 UIHosting 控制器,以便看到它们下面的背景图像。下面的代码仍然在我的 SwiftUI 视图下显示一个白色矩形。Paul Hudson要使用 edgeIgnoringSafeArea 但我不能使用它,因为我的 SwiftUIView 嵌入在更大的 UI 方案中。有什么办法可以使我看到的这个白色矩形清晰?

    var body: some View {
          ZStack {
            VStack {
                 //omitted
              }
        }.background(Color.clear)
   }
Run Code Online (Sandbox Code Playgroud)

Dáv*_*tor 7

如果您使用的是UIHostingController,你需要将设置backgroundColor其属性view.clear为好。

yourHostingController.view.backgroundColor = .clear
Run Code Online (Sandbox Code Playgroud)

  • 请注意,对推送视图的调用必须使用: view.modalPresentationStyle = .overFullScreen //用于透明度支持 v.window?.rootViewController?.present(view,animated: true) (2认同)