ios SwiftUI:方向更改动画期间的FullScreenCover背景颜色

svmlkihl  于 2023-02-10  发布在  iOS
关注(0)|答案(1)|浏览(205)

使用.fullScreenCover() ViewModifier并设置其背景时,在方向更改动画期间不会考虑该背景。

如何在更改方向时消除白色背景?

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())
    }
}

在此特定情况下,在调整视图大小期间(在单独的窗口中打开gif),它(部分)显示白色背景:

yx2lnoni

yx2lnoni1#

多亏了this answer,才找到了解决方案。
您需要获取底层的UIKit视图并将其背景色更改为.clear(默认情况下似乎是.white,如果苹果可以更改它,那就太好了):

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) {}
}

然后将其应用于SwiftUI视图:

.background(BackgroundTransparentView())

相关问题