我有一个带有节页眉和页脚的UITableView
。在一个单元格中,我有一个 UITextField,它会触发键盘的显示。
当键盘出现时,只有有时(这在某种程度上取决于表格的滚动位置)最后一节页脚会正确向上移动,因此该页脚出现在键盘
的正上方。
但是当我再次隐藏键盘时,页脚会保留在这个位置,直到通过进一步的用户交互刷新表格。我怎样才能避免这种情况,或者至少以编程方式强制刷新?
tableView.reloadData()
没有帮助。
请注意,它仅发生在 iPhone 上,而不发生在 iPad 上。到目前为止仅在 iOS 12.2 上进行了测试。
最佳答案
override func viewWillAppear(_ animated: Bool) {
self.registerForKeyboardNotifications()
super.viewWillAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
self.deregisterFromKeyboardNotifications()
super.viewWillDisappear(animated)
}
func registerForKeyboardNotifications(){
//Adding notifies on keyboard appearing
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name:
UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
func deregisterFromKeyboardNotifications(){
//Removing notifies on keyboard appearing
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWasShown(notification: NSNotification){
//Need to calculate keyboard exact size due to Apple suggestions
var info = notification.userInfo!
let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
//let contentInsets : UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize!.size.height, right: 0.0)
}
@objc func keyboardWillBeHidden(notification: NSNotification){
//Once keyboard disappears, restore original positions
//var info = notification.userInfo!
//let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
//let contentInsets : UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: -keyboardSize!.height, right: 0.0)
tableview.transform = CGAffineTransformMakeScale(1, -1);
}
关于ios - 显示和隐藏键盘时 UITableView 部分页脚出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55632275/