简体   繁体   English

显示和隐藏键盘时,UITableView节的页脚问题

[英]UITableView section footer issues when showing and hiding a keyboard

I have a UITableView with section headers and footers. 我有一个带有部分页眉和页脚的UITableView In one cell I have a UITextField, which triggers the display of a keyboard. 在一个单元格中,我有一个UITextField,它触发键盘的显示。

When the keyboard appears, only sometimes (it somehow depends on the scrolling position of the table) the last section footer is correctly moved upwards, so this footer appears just above the keyboard . 当出现键盘时,仅有时(它在某种程度上取决于表格的滚动位置)最后一个部分的页脚会正确向上移动,因此此页脚会出现在keyboard上方。

But when I hide the keyboard again, the footer stays in this place, until the table is refreshed by further user interaction. 但是,当我再次隐藏键盘时,页脚将停留在该位置,直到通过进一步的用户交互刷新表格为止。 How can I avoid that or at least programmatically enforce the refresh? 如何避免这种情况或至少以编程方式强制执行刷新?

tableView.reloadData() does not help. tableView.reloadData()没有帮助。

Note that it only happens on the iPhone, not on iPad. 请注意,它仅在iPhone上发生,而不在iPad上发生。 Tested so far with iOS 12.2 only. 到目前为止,仅使用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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM