gpt4 book ai didi

ios - 文本字段keyboardType在SwiftUI中不起作用

转载 作者:行者123 更新时间:2023-12-01 19:30:31 26 4
gpt4 key购买 nike

我制作了一个 customTextFieldClass ,并在我的应用程序中为Modifier添加了TextField。但是问题是.keyBoardTypeTextField中不起作用。

  • CustomTextField()
  • struct CustomTextField: UIViewRepresentable {

    var tag:Int = 0
    var placeholder:String?
    @Binding var strText:String

    class MoveToNextTextField: NSObject, UITextFieldDelegate {
    @Binding var strText: String

    init?(strText: Binding<String>) {
    _strText = strText
    }

    required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if let nextField = textField.superview?.superview?.viewWithTag(textField.tag + 1) as? UITextField {
    nextField.becomeFirstResponder()
    } else {
    textField.resignFirstResponder()
    }
    return false
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let currentText = textField.text ?? ""
    guard let stringRange = Range(range, in: currentText) else { return false }
    let updatedText = currentText.replacingCharacters(in: stringRange, with: string)
    textField.text = updatedText
    return true
    }
    func textFieldDidChangeSelection(_ textField: UITextField) {
    strText = textField.text ?? ""
    }
    }

    func makeUIView(context: UIViewRepresentableContext<CustomTextField>) -> UITextField {
    let tmpFeild = UITextField(frame: .zero)
    tmpFeild.tag = tag
    tmpFeild.delegate = context.coordinator
    tmpFeild.placeholder = placeholder
    return tmpFeild
    }
    func makeCoordinator() -> CustomTextField.MoveToNextTextField {
    return MoveToNextTextField(strText:$strText)!
    }
    func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<CustomTextField>) {
    uiView.text = strText
    }
    }

  • MyTextFieldModifier()
  • struct MyTextFieldModifier: ViewModifier {
    func body(content: Content) -> some View {
    content
    .frame(height: 40.0).font(.custom(FONT.PoppinsRegular, size: 16)).background(RoundedRectangle(cornerRadius: 5).foregroundColor(Color("lightGreen")).padding(.horizontal, -10.0)).padding(.horizontal,45).accentColor(Color("ThemeGreenColor"))
    }
    }
  • 代码
  •      CustomTextField(tag: 1, placeholder: "Phone",strText:self.$txtPhone)
    .keyboardType(.numberPad)
    .modifier(MyTextFieldModifier())
    CustomTextField(tag: 2, placeholder: "Email",strText:self.$txtEmail)
    .keyboardType(.emailAddress)
    .modifier(MyTextFieldModifier())
  • 图片

  • TesxtField editing begin

    最佳答案

    SwiftUI标准.keyboardType是用于SwiftUI标准TextField的。如果使用UIKit UITextField,则必须使用相应的UIKit UIKeyboardType
    因此,下面是使用的固定变体(已通过Xcode 11.4 / iOS 13.4测试)

    CustomTextField(tag: 1, placeholder: "Phone", 
    strText:self.$txtPhone, keyboardType: .phonePad)
    demo
    和代码
    struct CustomTextField: UIViewRepresentable {

    var tag:Int = 0
    var placeholder:String?
    @Binding var strText:String
    var keyboardType = UIKeyboardType.default

    class MoveToNextTextField: NSObject, UITextFieldDelegate {
    @Binding var strText: String

    init?(strText: Binding<String>) {
    _strText = strText
    }

    required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if let nextField = textField.superview?.superview?.viewWithTag(textField.tag + 1) as? UITextField {
    nextField.becomeFirstResponder()
    } else {
    textField.resignFirstResponder()
    }
    return false
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let currentText = textField.text ?? ""
    guard let stringRange = Range(range, in: currentText) else { return false }
    let updatedText = currentText.replacingCharacters(in: stringRange, with: string)
    textField.text = updatedText
    return true
    }
    func textFieldDidChangeSelection(_ textField: UITextField) {
    strText = textField.text ?? ""
    }
    }

    func makeUIView(context: UIViewRepresentableContext<CustomTextField>) -> UITextField {
    let tmpFeild = UITextField(frame: .zero)
    tmpFeild.tag = tag
    tmpFeild.delegate = context.coordinator
    tmpFeild.placeholder = placeholder
    tmpFeild.keyboardType = keyboardType
    return tmpFeild
    }
    func makeCoordinator() -> CustomTextField.MoveToNextTextField {
    return MoveToNextTextField(strText:$strText)!
    }
    func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<CustomTextField>) {
    uiView.text = strText
    }
    }

    关于ios - 文本字段keyboardType在SwiftUI中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63067661/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com