添加键盘上方浮动完成按钮

This commit is contained in:
2026-07-13 22:01:16 +08:00
parent 51fb757143
commit df7e74b10e
2 changed files with 67 additions and 1 deletions
+33
View File
@@ -11,6 +11,8 @@ class FormatLogViewController: UIViewController {
private var textView: UITextView!
private var formatButton: UIButton!
private var floatingDoneButton: UIButton!
private var floatingDoneButtonBottomConstraint: NSLayoutConstraint!
private var doneButton: UIBarButtonItem!
private var pasteButton: UIBarButtonItem!
private var resetButton: UIBarButtonItem!
@@ -65,7 +67,28 @@ class FormatLogViewController: UIViewController {
formatButton.addTarget(self, action: #selector(didTapFormatButton), for: .touchUpInside)
view.addSubview(formatButton)
var doneConfig = UIButton.Configuration.glass()
doneConfig.title = "完成"
doneConfig.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
return outgoing
}
doneConfig.baseForegroundColor = .label
doneConfig.cornerStyle = .capsule
doneConfig.contentInsets = NSDirectionalEdgeInsets(top: 15, leading: 22, bottom: 15, trailing: 22)
floatingDoneButton = UIButton(configuration: doneConfig)
floatingDoneButton.translatesAutoresizingMaskIntoConstraints = false
floatingDoneButton.alpha = 0
floatingDoneButton.addTarget(self, action: #selector(didTapFloatingDoneButton), for: .touchUpInside)
view.addSubview(floatingDoneButton)
floatingDoneButtonBottomConstraint = floatingDoneButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -16)
NSLayoutConstraint.activate([
floatingDoneButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16),
floatingDoneButtonBottomConstraint,
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 16),
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
@@ -82,6 +105,10 @@ class FormatLogViewController: UIViewController {
textView.resignFirstResponder()
}
@objc private func didTapFloatingDoneButton() {
textView.resignFirstResponder()
}
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)
@@ -98,6 +125,9 @@ class FormatLogViewController: UIViewController {
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
self.textView.contentInset.bottom = keyboardHeight
self.textView.verticalScrollIndicatorInsets.bottom = keyboardHeight
self.floatingDoneButtonBottomConstraint.constant = -(keyboardHeight + 8)
self.floatingDoneButton.alpha = 1
self.view.layoutIfNeeded()
}
}
@@ -107,6 +137,9 @@ class FormatLogViewController: UIViewController {
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
self.textView.contentInset.bottom = 0
self.textView.verticalScrollIndicatorInsets.bottom = 0
self.floatingDoneButtonBottomConstraint.constant = -16
self.floatingDoneButton.alpha = 0
self.view.layoutIfNeeded()
}
}