0
我正在開發一個聊天應用程序,並且正在嘗試讓用戶交互式地關閉鍵盤。不幸的是,它不工作,因爲我預料到:關閉鍵盤的平滑過渡
(這是我開始拉鍵盤下來,我想我的文字,以便留在鍵盤的頂部不管是什麼。這是我曾嘗試:
在viewDidLoad
:
textView.keyboardDismissMode = .interactive//I have tried the same thing with the table view as well, but it produces the same result.
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
在同一個班級:
func keyboardWillShow(_ aNotification: NSNotification) {
let info = aNotification.userInfo!
let keyboardFrame = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue()
UIView.animate(withDuration: 0.25) {() -> Void in
self.messageBottomConstraint.constant = keyboardFrame.height
self.view.layoutIfNeeded()
}
}
func keyboardWillHide(_ aNotification: NSNotification) {
UIView.animate(withDuration: 0.25) {() -> Void in
self.messageBottomConstraint.constant = 0
self.view.layoutIfNeeded()
}
}
如何在文本視圖位於鍵盤頂部而用戶正在解除它的情況下?謝謝!
你能舉個例子嗎?博客文章在Obj-C中,我將其轉換成一些麻煩......謝謝! – penatheboss