有没有办法设置fullScreenCover背景不透明度?

我需要从视图组件内的按钮推送一个模态视图,但应该只覆盖屏幕高度的下半部分,上半部分是半透明背景(黑色,不透明度为 30%)。在 fullscreenCover 视图构建器中设置最顶层视图的不透明度不起作用。任何帮助,将不胜感激。

struct ContentView: View {
    
    @State var present: Bool = false
    var body: some View {
        
        VStack(spacing: 20) {
            
            Button(action: {
                present = true
            }, label: {
                
                Text("spawn translucent modal")
            })
            .fullScreenCover(isPresented: $present) {
                VStack(spacing: 20) {
                    Spacer()
                        .frame(maxWidth: .infinity, minHeight: 100)
                        .background(Color.black)
                        .opacity(0.3)
                    
                    Text("modal")
                }
                .background(Color.clear)
                
            }
            
            Text("some content")
            Text("some more content")
        }
    }
}

回答

这是可能的解决方案

        .fullScreenCover(isPresented: $present) {
            VStack(spacing: 20) {
                Spacer()
                    .frame(maxWidth: .infinity, minHeight: 100)
                    .background(Color.black)
                    .opacity(0.3)
                
                Text("modal")
            }
            .background(BackgroundCleanerView())     // << helper !!
        }

现在是帮手

struct BackgroundCleanerView: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        let view = UIView()
        DispatchQueue.main.async {
            view.superview?.superview?.backgroundColor = .clear
        }
        return view
    }

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


以上是有没有办法设置fullScreenCover背景不透明度?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>