【问题标题】:How to make the background of a fullscreencover transparent?如何使全屏封面的背景透明?
【发布时间】:2023-01-07 10:07:17
【问题描述】:

这就是我想要实现的:

这就是我得到的:

我尝试将 VStack 嵌入到另一个带有 .background(.gray.opacity(90))VStack 中,但它没有做任何事情。我正在使用fullScreenCover

let width = UIScreen.main.bounds.width
.fullScreenCover(isPresented: $showCover) {
BuyWithPointsView(type: type).frame(width: (width * (91.733 / 100)), height: (width * (66.667 / 100)))
}

编辑: 我尝试实现这个答案:SwiftUI: Translucent background for fullScreenCover

.fullScreenCover(isPresented: $showCover) {
                    ZStack {
                        ZStack {
                            Color.gray.opacity(0.1).edgesIgnoringSafeArea(.all)
                        }.background(BackgroundBlurView())
                        BuyWithPointsView(type: type).frame(width: (width * (91.733 / 100)), height: (width * (66.667 / 100)))
                    }
                }
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) {}
}

这导致了这个结果: 然而这个结果并没有像我想要的那样真正让背景看透。改变不透明度和颜色并没有太大作用。

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    我找到了透明背景的更清洁解决方案,没有任何闪烁问题。

    struct ClearBackgroundView: UIViewRepresentable {
        func makeUIView(context: Context) -> UIView {
            return InnerView()
        }
        
        func updateUIView(_ uiView: UIView, context: Context) {
        }
        
        private class InnerView: UIView {
            override func didMoveToWindow() {
                super.didMoveToWindow()
                
                superview?.superview?.backgroundColor = .clear
            }
            
        }
    }
    

    用法

    PresenterView()
        .fullScreenCover(isPresented: $isPresented) {
            PresentedView()
                .background(ClearBackgroundView())
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 2017-10-23
      • 2015-08-06
      • 2011-09-17
      相关资源
      最近更新 更多