0

I have a UITableView with section headers and footers. In one cell I have a UITextField, which triggers the display of a keyboard.

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.

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.

Note that it only happens on the iPhone, not on iPad. Tested so far with iOS 12.2 only.

Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
LPG
  • 384
  • 2
  • 12
  • 2
    Use https://github.com/hackiftekhar/IQKeyboardManager for automatic keyboard management. Easy to use and also having good ratting – Hitesh Surani Apr 11 '19 at 12:19
  • @LPG Please check my answer. And also i recommendation to use IQKeyboadManager (https://github.com/hackiftekhar/IQKeyboardManager). – Bhavesh Nayi Apr 11 '19 at 14:19
  • @HiteshSurani how did you solve this issue? It's happening to me on iOS 13.6 even I am using `IQKeyboardMananger` – Anirudha Mahale Nov 02 '20 at 05:54

1 Answers1

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

}
Bhavesh Nayi
  • 705
  • 4
  • 15