本文介绍了SwiftUI:fullScreenCover的半透明背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以从技术上讲,我想显示一个加载屏幕视图.我正在使用 fullScreenCover .

So technically I want to show a loading screen view. I'm using fullScreenCover.

struct ContentView: View {

    @State private var isLoading = false

    var body: some View {
        VStack {
            Text("Hello there")
            Button("Start loading") {
                isLoading.toggle()
            }
            .fullScreenCover(isPresented: $isLoading) {
                ZStack{
                    Color.black.opacity(0.5).edgesIgnoringSafeArea(.all)
                    VStack {
                        ProgressView()
                        Button("Stop loading") {
                            isLoading.toggle()
                        }
                    }
                }
            }
        }
    }
}

问题是我无法使此加载屏幕半透明. sheet popover 的行为相同.

The problem is that I cannot make this loading screen translucent. sheet or popover behave the same way.

推荐答案

以下是可能方法的演示.您可以根据需要调整视觉效果的参数.

Here is a demo of possible way. Parameters of visual effect you can tune for your needs.

在Xcode 12/iOS 14上进行了测试.

Tested with Xcode 12 / iOS 14.

       // ... other code
            .fullScreenCover(isPresented: $isLoading) {
                ZStack{
                    Color.black.opacity(0.5).edgesIgnoringSafeArea(.all)
                    VStack {
                        ProgressView()
                        Button("Stop loading") {
                            isLoading.toggle()
                        }
                    }
                }
                .background(BackgroundBlurView())
            }
        }
    }
}

struct BackgroundBlurView: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        let view = UIVisualEffectView(effect: UIBlurEffect(style: .light))
        DispatchQueue.main.async {
            view.superview?.superview?.backgroundColor = .clear
        }
        return view
    }

    func updateUIView(_ uiView: UIView, context: Context) {}
}

这篇关于SwiftUI:fullScreenCover的半透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 18:30