【问题标题】:UITableView section footer issues when showing and hiding a keyboard显示和隐藏键盘时的 UITableView 部分页脚问题
【发布时间】:2019-09-02 01:50:10
【问题描述】:

我有一个带有节页眉和页脚的UITableView。在一个单元格中,我有一个 UITextField,它触发键盘的显示。

当键盘出现时,只有有时(它以某种方式取决于表格的滚动位置)最后一节页脚正确地向上移动,所以这个页脚出现在 keyboard 的正上方。

但是当我再次隐藏键盘时,页脚会停留在这个位置,直到通过进一步的用户交互刷新表格。我怎样才能避免这种情况,或者至少以编程方式强制刷新?

tableView.reloadData() 没有帮助。

请注意,它只发生在 iPhone 上,而不是 iPad。到目前为止仅使用 iOS 12.2 进行了测试。

【问题讨论】:

标签: ios uitableview uitextfield uitableviewsectionheader


【解决方案1】:
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);

}

【讨论】:

  • 这段代码实现的思路是什么?
猜你喜欢
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
  • 2016-03-06
  • 2019-12-20
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多