iosswiftswiftuiuihostingcontrollerzstack

How to set UIHostingController's background color to clear?


I need to be able to see through my SwiftUI Views as well as the UIHosting Controller presenting them in order to see a background image underneath them. The code below still shows a white rectangle under my SwiftUI View. Paul Hudson says to use edgesIgnoringSafeArea but I cannot use that as my SwiftUIView is embedded in a larger UI scheme. Is there any way to make this white rectangle I am seeing clear?

    var body: some View {
          ZStack {
            VStack {
                 //omitted
              }
        }.background(Color.clear)
   }

Solution

  • If you are using a UIHostingController, you need to set the backgroundColor property of its view to .clear as well.

    yourHostingController.view.backgroundColor = .clear