我正在尝试在调用键盘时查看我的视图。我已经能够在原生iOS键盘上完成这项工作,但自定义键盘似乎有错误。第一次启动键盘时,视图向上移动的速度比应有的低55个像素。每次键盘启动后,第一次正常工作。
我能够找到类似的问题,但答案似乎对我不起作用:can't get correct value of keyboard height in iOS8
这是我目前用于推送视图的代码:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == self.partialView{
self.view.frame.origin.y -= keyboardSize.height
print(self.view.frame.origin.y)
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
print(self.view.frame.origin.y)
}
}
}
希望您可以通过键盘的第一次调用来帮助我解决问题。我试图从窗口引用转换键盘框架,但是,这也不成功。