编辑页增加键盘遮挡处理

This commit is contained in:
2026-07-16 21:13:52 +08:00
parent f1a380d105
commit 70870e1ec2
+35
View File
@@ -29,6 +29,41 @@ class ClipEditViewController: UIViewController {
setupNavigationBar() setupNavigationBar()
setupTextView() setupTextView()
setupKeyboardObservers()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
private func setupKeyboardObservers() {
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 keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
let keyboardFrameInView = view.convert(keyboardFrame, from: nil)
let intersection = keyboardFrameInView.intersection(view.bounds)
let keyboardHeight = intersection.height
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval ?? 0.25
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
self.textView.contentInset.bottom = keyboardHeight
self.textView.verticalScrollIndicatorInsets.bottom = keyboardHeight
self.view.layoutIfNeeded()
}
}
@objc private func keyboardWillHide(_ notification: Notification) {
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval ?? 0.25
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
self.textView.contentInset.bottom = 0
self.textView.verticalScrollIndicatorInsets.bottom = 0
self.view.layoutIfNeeded()
}
} }
override func viewDidAppear(_ animated: Bool) { override func viewDidAppear(_ animated: Bool) {