我需要从视图组件内部的按钮中推送模式视图,但只能覆盖屏幕高度的下半部分,上半部分为半透明背景(黑色,不透明度为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) {}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句