【发布时间】:2018-06-15 10:47:55
【问题描述】:
我想在显示键盘时向上移动屏幕,在隐藏键盘时移动到底部。
我使用这个代码:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func keyboardWillShow(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
}
}
}
@objc 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
}
}
}
但我有这个问题:http://gph.is/2AvLo1A
在这个屏幕上,我点击 textField 以显示键盘并点击空白空间以隐藏键盘几次。我的屏幕往下爬,黑色的背景填满了整个屏幕。如何解决?
更新:
显示键盘后,底部有新的空白区域。如果我滚动我可以看到它:https://imgur.com/a/fFyEC
【问题讨论】: