键盘弹出时,我有一个文本字段。为了解释这一点,我将内容视图包装在滚动视图中,并在显示键盘时调整其高度。
我将观察者设置为viewDidLoad:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)
显示/隐藏键盘的方法。
func keyboardWillShow(notification: NSNotification) {
adjustInsetForKeyboardShow(show: true, notification: notification)
}
func keyboardWillHide(notification: NSNotification) {
adjustInsetForKeyboardShow(show: false, notification: notification)
}
private func adjustInsetForKeyboardShow(show: Bool, notification: NSNotification) {
let userInfo = notification.userInfo ?? [:]
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let adjustmentHeight = ((keyboardFrame.height) * (show ? 1 : -1))
scrollView.contentInset.bottom += adjustmentHeight
scrollView.scrollIndicatorInsets.bottom += adjustmentHeight
}
使用 default
iOS键盘时,在模拟器/设备上,这似乎正常。
然而,当我使用第三方键盘(更具体地说是GBoard
)时,滚动视图会将高度调整为远远大于default
键盘的高度,并用大量白色拉伸视图空间。
notification.userInfo
属性帐户是否为密钥UIKeyboardFrameBeginUserInfoKey
的第三方键盘?
我不确定发生了什么