在某些情况下,当我抬起键盘时,会看到以下内容:
但是在不同的设备上,我得到了:
为什么会这样?
这是我用于键盘处理的代码:
//MARK: Keyboard handling
private func registerKeyboardNotifications(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide), name: UIResponder.keyboardDidHideNotification, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
self.view.layoutIfNeeded()
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0 {
self.view.frame.origin.y -= keyboardSize.height
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y = 0
}
}
@objc func keyboardDidShow(notification:NSNotification){
isKeyboardVisible = true
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
print("Keyboard size \(keyboardSize)")
}
}
@objc func keyboardDidHide(notification:NSNotification){
isKeyboardVisible = false
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
}
}
我的文本字段的secureTextEntry属性设置为true。