0

To provide a chat functionality, i use a tableview which displays each message in a row. When the keyboard is shown, i push the content offset y-value up. That way, the last message you can read is still visible when the keyboard is opened. The problem occurs when you open the emoji keyboard, it fire the keyboardWasShown function again and calculates it from the current position. This causes the tableview content go up higher than needed. What is a smart way to handle tableview content offset when the keyboard is shown / when different types of keyboards can be shown?

func keyboardWasShown(_ notification: Notification){
let oldOffset = self.tableViewChat?.tableView.contentOffset.y
    let info = (notification as NSNotification).userInfo
    let value = info?[UIKeyboardFrameEndUserInfoKey]

    if let rawFrame = (value as AnyObject).cgRectValue
    {
        let keyboardFrame = self.tableViewChat?.tableView.convert(rawFrame, from: nil)
        let keyboardHeight = keyboardFrame?.height
        self.bottomConstraint.constant = keyboardFrame!.size.height - (self.navigationController!.navigationBar.frame.height)
        self.sendButtonBottom.constant = self.bottomConstraint.constant

        let currentOffset = self.tableViewChat?.tableView.contentOffset.y

        self.tableViewChat?.tableView.setContentOffset(CGPoint(x: 0, y: (currentOffset! + keyboardHeight! - 52)), animated: true)
    }
}
Jochen Österreicher
  • 683
  • 2
  • 11
  • 25

0 Answers0