键盘上方增加确认按钮,点击验证拼写

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-07-20 21:43:36 +08:00
parent 9ce3991de2
commit 36cb048aa1
2 changed files with 21 additions and 0 deletions
@@ -2,6 +2,7 @@ import UIKit
final class SpellingInputView: UIView, UITextFieldDelegate {
var onChange: (() -> Void)?
var onSubmit: (() -> Void)?
private let hiddenField = UITextField()
private let stack = UIStackView()
@@ -56,6 +57,23 @@ final class SpellingInputView: UIView, UITextFieldDelegate {
hiddenField.frame = .zero
addSubview(hiddenField)
let accessory = UIView()
accessory.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 52)
accessory.backgroundColor = .clear
let confirmButton = UIButton(type: .system)
confirmButton.setTitle("确认", for: .normal)
confirmButton.titleLabel?.font = .systemFont(ofSize: 17, weight: .semibold)
confirmButton.addAction(UIAction { [weak self] _ in
self?.onSubmit?()
}, for: .touchUpInside)
confirmButton.translatesAutoresizingMaskIntoConstraints = false
accessory.addSubview(confirmButton)
NSLayoutConstraint.activate([
confirmButton.trailingAnchor.constraint(equalTo: accessory.trailingAnchor, constant: -8),
confirmButton.topAnchor.constraint(equalTo: accessory.topAnchor, constant: 8)
])
hiddenField.inputAccessoryView = accessory
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(reactivate)))
}