From 70870e1ec214dcec5cdef8b3592642215eafac42 Mon Sep 17 00:00:00 2001 From: fish Date: Thu, 16 Jul 2026 21:13:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=BE=91=E9=A1=B5=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E9=94=AE=E7=9B=98=E9=81=AE=E6=8C=A1=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Note/ClipEditViewController.swift | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Note/ClipEditViewController.swift b/Note/ClipEditViewController.swift index c07547f..eb3538c 100644 --- a/Note/ClipEditViewController.swift +++ b/Note/ClipEditViewController.swift @@ -29,6 +29,41 @@ class ClipEditViewController: UIViewController { setupNavigationBar() 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) {