KevinVuD
KevinVuD

Reputation: 611

Keyboard covers text view while typing in tableview cell

I have a text view inside my tableview cell and I use

self.tableView.beginUpdates()
self.tableView.endUpdates()

to update my cell height based on the amount of texts that user types and everything works pretty well except for the keyboard cover my text view area if my text view goes below the keyboard. I have tried

 NSNotification.Name.UIKeyboardWillShow

to push my table view up and think it is the solution but the problem still exist and it really doesn't help at all in this case. Is there a way to keep the text always above the keyboard while typing? Thank you all for helping me and below is how I update my table view cell height

//set textView delegate in custom cell and use textView didchange func
func textViewDidChange(_ textView: UITextView) {
  textDidChanges?()
}
//then in my cellForRowAt
cell.textDidChanges = {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}

keyboard cover text

cell height function works very well

Upvotes: 0

Views: 927

Answers (1)

arvinq
arvinq

Reputation: 701

Inside the method to execute when a UIResponder.keyboardWillShowNotification is invoked, get the keyboard's size and add it to the tableView and its scroll Indicator Insets.

 NotificationCenter.default.addObserver(self, selector: #selector(VC.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)

func keyboardWillShow(_ notification: Notification) {
   guard let info = notification.userInfo,
         let keyboardFrameRect = info[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue
          else { return }

   let keyboardRect = keyboardFrameRect.cgRectValue
   let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardRect.height, right: 0)
   tableView.contentInset = contentInset
   tableView.scrollIndicatorInsets = contentInset
}

Upvotes: 2

Related Questions