我的静态 UITableView 的单元格中有几个 UITextField。当获得焦点时,他们中的大多数都很好。
但是当某些字段获得焦点时(例如当我单击 Unit No 字段时),单元格会跳转并像这样覆盖键盘。
完全没有头绪。任何想法?谢谢。
最佳答案
尝试使用这个 pod https://github.com/hackiftekhar/IQKeyboardManager , 它将涵盖所有键盘场景。
如果您想使用自定义代码,您必须在 viewWillAppear 中订阅 UIKeyboardWillShow 和 UIKeyboardWillHide,并在 viewWillDisappear< 中取消订阅/强>。覆盖 touchesBegan 之后,这将处理您在屏幕上的所有触摸,您可以在此函数中结束编辑。
我的示例代码:
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: .UIKeyboardWillHide, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
UIView.animate(withDuration: 1, animations: {
self.view.bounds.origin.y += keyboardSize.height
}
}
}
func keyboardWillHide(notification: NSNotification) {
self.view.bounds.origin.y = 0
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
关于iOS 键盘在静态表格 View 单元格中覆盖了一些 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46518844/