在您的ChatViewController 中,您可以禁用IQKeyboardManager。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
IQKeyboardManager.shared().isEnabled = false
IQKeyboardManager.shared().isEnableAutoToolbar = false
}
当你要像这样离开那个视图控制器时,你可以重新启用它。
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
IQKeyboardManager.shared().isEnabled = true
IQKeyboardManager.shared().isEnableAutoToolbar = true
}
现在我们要禁用IQKeyboardManager,我们必须自己管理键盘。
为此,您可以尝试以下方法。
override func viewDidLoad() {
super.viewDidLoad()
prepareTableView()
observeKeyboardEvents()
}
这里调用viewDidLoad方法中的observeKeyboardEvents方法。
private func observeKeyboardEvents() {
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: nil) { [weak self] (notification) in
guard let keyboardHeight = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
print("Keyboard height in KeyboardWillShow method: \(keyboardHeight.height)")
self?.tableView.contentInset.bottom = keyboardHeight.height + 8
self?.tableView.scrollIndicatorInsets.bottom = keyboardHeight.height + 8
}
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) { [weak self] (notification) in
self?.tableView.scrollIndicatorInsets.bottom = 0 + 8
self?.tableView.contentInset.bottom = 0 + 8
}
}
在observeKeyboardEvents 方法中,您注册此视图控制器以观察键盘Appear 和Disappear 通知并相应地调整您的tableView 的contentInset.bottom。所以我们也应该移除这个视图控制器来停止接收键盘事件通知。为此,您可以尝试以下代码。
deinit {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
}
希望,这可能对您有所帮助。 :)