当模态显示键盘时,SwiftUI 挤压父视图

SwiftUI squeezes parent view when modal shows keyboard

在使用 SwiftUI 时,我注意到一个非常不寻常的行为。在 iOS 14 上,当在模态屏幕上显示键盘时,父视图也会被挤压。为了表明我的意思,我制作了一个简短的演示视图和一个显示问题的简短 gif。你可以发现 here.

struct SqueezTestView: View {
    @State var isModalPresented = false
    @State var text = ""
    @State var colors: [Color] = [.red, .green, .blue, .orange, .pink, .purple, .yellow]

    var body: some View {
        NavigationView {
            VStack {
                ForEach(0..<colors.count) { index in
                    colors[index]
                        .animation(.linear(duration: 1))
                }
            }
            .navigationBarTitle("Squeez")
            .navigationBarItems(leading: Button("Shuffel") { colors.shuffle() }, trailing: Button("Modal") { isModalPresented = true })
            .sheet(isPresented: $isModalPresented) {
                NavigationView {
                    VStack {
                        TextField("Test", text: $text, onCommit: {isModalPresented = false})
                            .padding()
                        Spacer()
                    }
                    .navigationBarTitle("Modal", displayMode: .inline)
                    .navigationBarItems(trailing: Button("Done") { isModalPresented = false })
                }
            }
        }
    }
}

我预计只有模态视图被挤压而父视图保持不变。在这种情况下,每次我在键盘可见时关闭模态时,父视图都会生成动画。有什么我遗漏的吗?这个问题有什么已知的修复方法吗?

我找到了问题的解决方案 。关键是设置

.ignoresSafeArea(.keyboard, edges: .bottom)

在父视图上。