![](/img/trans.png)
[英]SwiftUI Custom TextField with UIViewRepresentable Issue with ObservableObject and pushed View
[英]Custom TextField keyboard in SwiftUI with UIViewRepresentable
我在 UIViewRepresentable 中使用自定义 UITextField,以拥有一个只有小数点和自定义键盘工具栏的文本字段。 目标是向工具栏添加基本内容,例如插入减号、E 等……我见过很多只针对“完成”按钮的代码。 我已经尝试将 insertText() 添加到按钮操作,但我认为没有调用协调器,因此文本没有更新。 我想要的只是在光标位置插入自定义字符串的能力。
这是代码:
struct DataTextField: UIViewRepresentable {
private var placeholder: String
@Binding var text: String
init(_ placeholder: String, text: Binding<String>) {
self.placeholder = placeholder
self._text = text
}
func makeUIView(context: Context) -> UITextField {
let textfield = UITextField()
textfield.keyboardType = .decimalPad
textfield.delegate = context.coordinator
textfield.placeholder = placeholder
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: textfield.frame.size.width, height: 44))
let minusButton = UIBarButtonItem(title: "-", style: .plain, target: self, action: #selector(textfield.minusButtonTapped(button:)))
let scientificButton = UIBarButtonItem(title: "E", style: .plain, target: self, action: #selector(textfield.scientificButtonTapped(button:)))
toolBar.items = [minusButton, scientificButton]
toolBar.setItems([minusButton, scientificButton], animated: true)
textfield.inputAccessoryView = toolBar
textfield.borderStyle = .roundedRect
textfield.textAlignment = .right
textfield.adjustsFontSizeToFitWidth = true
return textfield
}
func updateUIView(_ uiView: UITextField, context: Context) {
uiView.text = text
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UITextFieldDelegate {
var parent: DataTextField
init(_ textField: DataTextField) {
self.parent = textField
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let currentValue = textField.text as NSString? {
let proposedValue = currentValue.replacingCharacters(in: range, with: string) as String
self.parent.text = proposedValue
}
return true
}
}
}
extension UITextField {
@objc func minusButtonTapped(button:UIBarButtonItem) -> Void {
insertText("-")
}
@objc func scientificButtonTapped(button:UIBarButtonItem) -> Void {
insertText("E")
}
}
以及问题的视频:
我发现这是一个 Swift 错误,只有在文本不在数组内时才会更新文本。 (就我而言,它是)经过数小时的尝试,我发现调用delegate?.textFieldDidBeginEditing?(self)
然后更新该函数内的文本会使按钮按预期工作。
extension UITextField {
@objc func minusButtonTapped(button: UIBarButtonItem) -> Void {
insertText("-")
delegate?.textFieldDidBeginEditing?(self)
}
@objc func scientificButtonTapped(button: UIBarButtonItem) -> Void {
insertText("E")
delegate?.textFieldDidBeginEditing?(self)
}
}
将此添加到 UIViewRepresentable 内的 Coordinator 类:
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
textField.resignFirstResponder()
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.parent.text = textField.text!
}
调用.resignFirstResponder()
是必须的。 如果键盘没有关闭,应用程序将崩溃,因为在我的情况下,视图本身可以被关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.