首页 > 解决方案 > 为什么在 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()
                }
            }

        }
    }
}

由于某种原因,安全区域在过渡完成后会改变颜色。
为什么会发生这种情况,我可以做些什么来解决这个问题?

标签: swiftswiftui

解决方案


另一种解决方案是移动框架以修改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)
    }
}

推荐阅读