【问题标题】:Swift 5 NumberPad extended buttons unresponsiveSwift 5 NumberPad 扩展按钮无响应
【发布时间】:2020-09-29 16:48:15
【问题描述】:

我在数字键盘上有一个完成按钮,当我输入一个值并按下完成时,该值没有保存/注册。我的代码如下:

我正在使用 UIRepresentable 协议(由 Rajeev Kumar S 提供)

struct TestTextfield: UIViewRepresentable {
    @Binding var text: String
    var keyType: UIKeyboardType
    func makeUIView(context: Context) -> UITextField {
        let textfield = UITextField()
      textfield.keyboardType = keyType
        let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: textfield.frame.size.width, height: 44))
        let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(textfield.doneButtonTapped(button:)))
        toolBar.items = [doneButton]
        toolBar.setItems([doneButton], animated: true)
        textfield.inputAccessoryView = toolBar
        return textfield
    }

    func updateUIView(_ uiView: UITextField, context: Context) {
        uiView.text = text

    }
}

extension  UITextField{
    @objc func doneButtonTapped(button:UIBarButtonItem) -> Void {
       self.resignFirstResponder()
    }

}

我在我的内容视图中得到了这个

struct ContentView : View {
@State var text = ""

var body: some View {
    TestTextfield(text: $text, keyType: UIKeyboardType.phonePad)
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 50)
        .overlay(
            RoundedRectangle(cornerRadius: 16)
                .stroke(Color.blue, lineWidth: 4)
    )
}

这是我正在使用的:

  • 斯威夫特 5
  • Xcode 11.5

Xcode 11.5

【问题讨论】:

    标签: ios swift xcode numpad


    【解决方案1】:

    你只需要将你的可代表类更新为这个!

    struct TestTextfield: UIViewRepresentable {
    
            @Binding var text: String
    
            var keyType: UIKeyboardType
    
            func makeUIView(context: Context) -> UITextField {
                let textfield = UITextField()
              textfield.keyboardType = keyType
                let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: textfield.frame.size.width, height: 44))
                let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(textfield.doneButtonTapped(button:)))
                toolBar.items = [doneButton]
                toolBar.setItems([doneButton], animated: true)
                textfield.inputAccessoryView = toolBar
                textfield.delegate = context.coordinator
                return textfield
            }
    
            func updateUIView(_ uiView: UITextField, context: Context) {
    
                uiView.text = text
            }
    
            func makeCoordinator() -> Coordinator {
                Coordinator($text)
            }
    
            class Coordinator: NSObject, UITextFieldDelegate {
    
                @Binding var text: String
    
                init(_ text: Binding<String>) {
                    self._text = text
                }
    
                func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
                    text = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) ?? ""
    
                    return true
                }
            }
        }
    

    试试吧!

    【讨论】:

      猜你喜欢
      • 2015-08-07
      • 2023-04-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多