ios - 显示和隐藏键盘时 UITableView 部分页脚出现问题

标签 ios uitableview uitextfield uitableviewsectionheader

我有一个带有节页眉和页脚的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/

相关文章:

ios - TextField 圆角半径 : Not Sharp Corners

ios - 实例方法 'xyz()'几乎匹配协议(protocol) 'xyz()'的可选要求 'ABDelegate'

ios - 继续到场景 B 的场景 A 如何知道用户何时完成场景 B 并再次显示场景 A?

ios - 在 TableView 中删除或重新排序单元格时的动画

ios - 在一个表格 View 单元格中创建一个 UISwitch - UISwitch 被复制

swift - UITableview 在 View 顶部添加空白

iphone - 如何在 iphone 应用程序中验证 URL

ios - 内容偏移动画损坏

ios - 屏幕空间光线转换

objective-c - 在 UITextView 中使用特定键盘的错误