为什么过渡在SwiftUI中结束后,为什么要应用edgeIgnoringSafeArea修饰符?

我想用不透明过渡覆盖UIScreen。这是我的看法:

struct ContentView: View {
    @State private var overlayUIScreen: Bool = false

    var body: some View {
        ZStack {
            if overlayUIScreen {
                Rectangle()
                    .edgesIgnoringSafeArea(.top)
                    .frame(width: UIScreen.main.bounds.size.width,height: UIScreen.main.bounds.size.height)
                    .foregroundColor(Color.gray)

                    .transition(.opacity)
            }

            Button("Overlay?") {
                withAnimation {
                    self.overlayUIScreen.toggle()
                }
            }

        }
    }
}

由于某种原因,安全区域在转换完成后会更改颜色。
为什么会发生这种情况,我该怎么做才能解决此问题?

cnnxl 回答:为什么过渡在SwiftUI中结束后,为什么要应用edgeIgnoringSafeArea修饰符?

另一种解决方案是移动框架以修改ZStack而不是Rectangle

struct ContentView: View {
    @State private var overlayUIScreen: Bool = false

    var body: some View {
        ZStack {
            if overlayUIScreen {
                Rectangle()
                    .edgesIgnoringSafeArea(.top)
                    .foregroundColor(Color.gray)
                    .transition(.opacity)
            }

            Button("Overlay?") {
                withAnimation {
                    self.overlayUIScreen.toggle()
                }
            }
        }
        .frame(width: UIScreen.main.bounds.size.width,height: UIScreen.main.bounds.size.height)
    }
}
,

一种解决方法是将您的视图包含在层次结构中,并将其不透明性归因于@State私有变量,如下所示:

struct ContentView: View {
    @State private var overlayOpacity: Double = 0.0

    var body: some View {
        ZStack {

            Rectangle()
                 .edgesIgnoringSafeArea(.top)
                 .frame(width: UIScreen.main.bounds.size.width,height: UIScreen.main.bounds.size.height)

                 .opacity(overlayOpacity)

                 .foregroundColor(Color.gray)

            Button("Overlay?") {
                withAnimation {
                    if self.overlayOpacity == 0.0 {
                        self.overlayOpacity = 1.0
                    } else {
                        self.overlayOpacity = 0.0
                    }
                }
            }

        }  .transition(.opacity)
    }
}
本文链接:https://www.f2er.com/3140116.html

大家都在问