我想将该功能实现到我的文本字段的功能中。我尝试了不同的方法,但我无法成功。
这是代码:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0 {
self.view.frame.origin.y -= keyboardSize.height
}
}
}
最佳答案
您使用的键 UIResponder.keyboardFrameBeginUserInfoKey
导致您得到错误的高度。
改用这个键:
UIResponder.keyboardFrameEndUserInfoKey
关于ios - 如何将 "move view with keyboard"函数实现到文本字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53967740/