2

When using a .fullScreenCover() ViewModifier and setting its background, that background is not respected during the orientation change animation.

How can I get rid of the white background during orientation change?

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Spacer()
            HStack {
                Spacer()
            }
        }
        .fullScreenCover(isPresented: .constant(true)) {
            VStack {
                Spacer()
                HStack {
                    Spacer()
                }
            }
            .background(Color.black.ignoresSafeArea())
        }
        .background(Color.red.ignoresSafeArea())
    }
}

In this specific case it shows (partially) a white background during view resizing (open the gif in a separate window):

enter image description here

1 Answers1

1

Just found the solution thanks to this answer.

You need to get hold of the underlying UIKit View and change its background color to .clear (it seems to be .white as default, which would be nice if Apple could change it):

struct BackgroundClearView: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        let view = UIView()
        DispatchQueue.main.async {
            view.superview?.superview?.backgroundColor = .clear
            view.superview?.superview?.layer.removeAllAnimations()
        }
        return view
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {}
}

and then apply it to your SwiftUI View:

.background(BackgroundTransparentView())
  • While this probably works you should use it with a caution - it is not granted that view hierarchy will not change with the iOS update and as such this may lead to various problems in the future. – Ivan Ičin Feb 08 '23 at 12:01