edgeIgnoringSafeArea.all中断键盘响应程序SwiftUI

问题描述

键盘响应程序文件如下:

class KeyboardResponder: ObservableObject {

    @Published var currentHeight: CGFloat = 0

    var _center: NotificationCenter

    init(center: NotificationCenter = .default) {
        _center = center
        _center.addobserver(self,selector: #selector(keyBoardWillShow(notification:)),name: UIResponder.keyboardWillShowNotification,object: nil)
        _center.addobserver(self,selector: #selector(keyBoardWillHide(notification:)),name: UIResponder.keyboardWillHideNotification,object: nil)
    }

    @objc func keyBoardWillShow(notification: Notification) {
        if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            withAnimation {
                currentHeight = keyboardSize.height
            }
        }
        print("the KEYBOARD HEIGHT IS \(self.currentHeight)")
    }

    @objc func keyBoardWillHide(notification: Notification) {
        withAnimation {
            currentHeight = 0
        }
        print("the KEYBOARD HEIGHT IS \(self.currentHeight)")
    }
}

我尝试在主体位于的视图中使用它:

vstack {
    vstack {
        \\view content here
    }.offset(y: -self.keyboardResponder.currentHeight) \\ keyboardResponder is an instance of KeyboardResponder
}.edgesIgnoringSafeArea(.all)

当我删除edgesIgnoringSafeArea(.all)时,它可以正常工作,但是如果我将其放进去,它将破坏偏移量,因此它根本不会移动内容...

解决方法

他们在iOS 14中弃用了.edgesIgnoreSafeArea。新方法为安全区域的“类型”提供了多个选项,可以忽略:.container(通常的“安全区域”)、. keyboard(新!)和.all(忽略容器和键盘-我怀疑这就是您得到的行为。

请改用.ignoresSafeArea(.container)。

https://developer.apple.com/documentation/swiftui/offsetshape/ignoressafearea(_:edges:)