iOS 14键盘响应器不再起作用

时间:2020-09-30 08:24:10

标签: swiftui keyboard offset ios14


在具有IOS 14的设备上,该设备检测到“键盘高度”以偏移/将填充设置为TextField的内容,并且下面的其他内容不再起作用。 IOS 14会自动执行此操作,但只会对文本字段执行此操作,而不会对文本字段下方的按钮执行此操作。 但我希望它像这样工作,使“下一个”按钮可见:

Pre Show Keyboard

Keyboard showing

IOS 14更改

在IOS 14中,您可以使用此命令停止偏移量

.ignoresSafeArea(.keyboard, edges: .bottom)

但是KeyboardResponder不能再在IOS 14上使用,所以我不能在内容中添加填充或偏移量。

final class KeyboardResponder: ObservableObject {
    
    private var notificationCenter: NotificationCenter
    @Published private(set) var currentHeight: CGFloat = 0
    
    init(center: NotificationCenter = .default) {
        notificationCenter = center
        notificationCenter.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }

    deinit {
        notificationCenter.removeObserver(self)
    }

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

    @objc func keyBoardWillHide(notification: Notification) {
        currentHeight = 0
    }
}

有人可以帮助更新Responder与IOS 14一起使用吗? 巴斯蒂

0 个答案:

没有答案