[英]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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.