Swift 4 显示/隐藏键盘查看
Swift 4 show / hide keyboard going over view
我正在尝试在我的键盘上实现显示/隐藏,但问题是当我显示我的键盘时它会越过我的视图,更重要的是它会越过我的文本字段和按钮,所以我可以输入,我只是无法按下我的按钮。
键盘前的截图如下:
这是键盘出现时的样子:
如您所见,我看不到文本字段和按钮。
这是我 viewDidLoad
中的代码
NotificationCenter.default.addObserver(self, selector: #selector(CommentsController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(CommentsController.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
}
}
}
为什么我的键盘越过我的视线?我该如何解决?
您需要将 UIKeyboardFrameBeginUserInfoKey
替换为 UIKeyboardFrameEndUserInfoKey
我正在尝试在我的键盘上实现显示/隐藏,但问题是当我显示我的键盘时它会越过我的视图,更重要的是它会越过我的文本字段和按钮,所以我可以输入,我只是无法按下我的按钮。
键盘前的截图如下:
这是键盘出现时的样子:
如您所见,我看不到文本字段和按钮。
这是我 viewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(CommentsController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(CommentsController.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
}
}
}
为什么我的键盘越过我的视线?我该如何解决?
您需要将 UIKeyboardFrameBeginUserInfoKey
替换为 UIKeyboardFrameEndUserInfoKey