【问题标题】:SwiftUI squeezes parent view when modal shows keyboard当模态显示键盘时,SwiftUI会挤压父视图
【发布时间】:2021-01-05 03:36:48
【问题描述】:

在使用 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 })
                }
            }
        }
    }
}

我希望只有模态视图被压缩,而父视图保持不变。在这种情况下,每次我在键盘可见时关闭模式时,父视图都会被动画化。有什么我遗漏的或者有任何已知的解决这个问题的方法吗?

【问题讨论】:

    标签: swift swiftui ios14


    【解决方案1】:

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

    .ignoresSafeArea(.keyboard, edges: .bottom)
    

    在父视图上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2014-11-23
      • 2020-04-16
      • 1970-01-01
      • 2022-01-04
      相关资源
      最近更新 更多