编辑页完成按钮改为玻璃浮动效果

This commit is contained in:
2026-07-16 21:26:36 +08:00
parent 255f171aa8
commit 4088db8ec0
+29 -15
View File
@@ -10,6 +10,8 @@ import UIKit
class ClipEditViewController: UIViewController {
private var textView: UITextView!
private var floatingDoneButton: UIButton!
private var floatingDoneButtonBottomConstraint: NSLayoutConstraint!
private var clip: Clip
init(clip: Clip) {
@@ -52,6 +54,8 @@ class ClipEditViewController: UIViewController {
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
self.textView.contentInset.bottom = keyboardHeight
self.textView.verticalScrollIndicatorInsets.bottom = keyboardHeight
self.floatingDoneButtonBottomConstraint.constant = -(keyboardHeight + 16)
self.floatingDoneButton.alpha = 1
self.view.layoutIfNeeded()
}
}
@@ -62,6 +66,8 @@ class ClipEditViewController: 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()
}
}
@@ -94,31 +100,39 @@ class ClipEditViewController: UIViewController {
textView.font = UIFont.preferredFont(forTextStyle: .body)
textView.textContainerInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
textView.text = clip.text
textView.inputAccessoryView = createInputAccessoryView()
view.addSubview(textView)
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)
let safeArea = view.safeAreaLayoutGuide
floatingDoneButtonBottomConstraint = floatingDoneButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -16)
NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: safeArea.topAnchor, constant: 16),
textView.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor, constant: 16),
textView.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -16),
textView.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor, constant: -16)
textView.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor, constant: -16),
floatingDoneButton.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -16),
floatingDoneButtonBottomConstraint
])
}
private func createInputAccessoryView() -> UIToolbar {
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 44))
toolbar.barStyle = .default
toolbar.isTranslucent = true
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "完成", style: .done, target: self, action: #selector(didTapKeyboardDoneButton))
toolbar.items = [flexSpace, doneButton]
toolbar.sizeToFit()
return toolbar
}
@objc private func didTapKeyboardDoneButton() {
@objc private func didTapFloatingDoneButton() {
textView.resignFirstResponder()
}