簡體   English   中英

顯示和隱藏鍵盤時,UITableView節的頁腳問題

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

我有一個帶有部分頁眉和頁腳的UITableView 在一個單元格中,我有一個UITextField,它觸發鍵盤的顯示。

當出現鍵盤時,僅有時(它在某種程度上取決於表格的滾動位置)最后一個部分的頁腳會正確向上移動,因此此頁腳會出現在keyboard上方。

但是,當我再次隱藏鍵盤時,頁腳將停留在該位置,直到通過進一步的用戶交互刷新表格為止。 如何避免這種情況或至少以編程方式強制執行刷新?

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);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM