あなたはUIKeyboardWillShowとUIKeyboardWillHide通知を登録:その後、
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
あなたは、キーボードのフレームを取得し、変更keyboardWillHideで、その後のtableView
func keyboardWillShow(notification: Notification) {
guard let keyboardFrame = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else { return }
let convertedFrame = view.convert(keyboardFrame, from: nil)
tableView.contentInset.bottom = convertedFrame.height
}
のインセットを変更keyboardWillShowに一番下を0に戻し、必要に応じてtableViewを特定の行にスクロールして戻します。これは、それをtableViewの最上部に位置する最初の行にスクロールします。
func keyboardWillHide(notification: Notification) {
tableView.contentInset.bottom = 0
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
}
それがレイアウトに影響を与えるコードを整頓あなたのTextFieldのデリゲートを保持しますので、それはまたそれを行うには良い方法です。
私は私はあなたが状況
deinit {
NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillHide, object: nil)
// If you don't have any other notifications you wan't to keep observing you can remove yourself from all observers by using this one line
// NotificationCenter.default.removeObserver(self)
}
静的であればフォームは動的か静的かを教えてください。次にコードなしでそれを達成するための非常に簡単な方法がありますか? –
私は[IQKeyboardManager](https://github.com/hackiftekhar/IQKeyboardManager)を使ってテキストフィールドを移動し、ブロックされないようにしました。これまでのところ、私にとってはかなり良い仕事をしてきました。 – NerdsGeeksGurus
キーボード通知を使用してそのような問題に対処する –