contentInset의 bottom을 키보드 높이만큼 올리면 됨
키보드 없어질 때는 bottom을 0으로 설정
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillHide),
name: UIResponder.keyboardWillHideNotification,
object: nil
)
@objc private func keyboardWillShow(_ notification: Notification) {
guard
let userInfo = notification.userInfo,
let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else {
return
}
textView.contentInset.bottom = keyboardFrame.height
textView.verticalScrollIndicatorInsets.bottom = keyboardFrame.height
}
@objc private func keyboardWillHide(_ notification: Notification) {
textView.contentInset.bottom = 0
textView.verticalScrollIndicatorInsets.bottom = 0
}
private let textView: UITextView = {
let textView = UITextView()
textView.font = UIFont.preferredFont(forTextStyle: .body)
textView.keyboardDismissMode = .interactive
return textView
}()
view.endEditing(true)