我正在开发一个聊天应用程序,我试图让用户以交互方式关闭键盘。不幸的是,它没有像我预期的那样工作:
(这是我开始拉下键盘。我希望我的文本视图无论如何都能保持在键盘顶部。这是我尝试过的:
在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()
}
}
如何在用户解雇时将文本视图保留在键盘顶部?谢谢!
答案 0 :(得分:1)
您应该覆盖$6$rounds=60000$ca.5CQtZct/vKXxo$nu.wS1OSYo6dz02zvo9QJTkzz2TEg9stQF3OsOvauGCTu36P6463P3Cmpron6dwK.Dz7.RT2Az56f9NbEcw.g1
上的inputAccessoryView
或inputAccessoryViewController
属性(UIResponder
继承)。这将允许您提供一个视图/视图控制器,当它出现时自动固定到键盘的顶部。
This blog post给出了如何执行此操作的示例。