1.继承 UITextViewDelegate
2.
NotificationCenter.default.addObserver(self, selector: #selector(TextViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(TextViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
3.
func keyboardWillShow(notification:NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
//self.contentTextView.frame.origin.y -= keyboardSize.height
self.contentTextView.contentInset.bottom = keyboardSize.height
}
}
func keyboardWillHide(notification:NSNotification) {
if ((notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue) != nil {
self.contentTextView.contentInset.bottom = 0
//keyboardIsPresent = false
}
}