简体   繁体   English

文本字段键盘类型在 SwiftUI 中不起作用

[英]Textfield keyboardType is not working in SwiftUI

I made a customTextFieldClass and also had been added a Modifier for TextField in my app.我制作了一个customTextFieldClass ,并且还在我的应用程序中添加了一个TextField Modifier But problem is .keyBoardType not working in 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() 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"))
    }
}
  • Code代码
     CustomTextField(tag: 1, placeholder: "Phone",strText:self.$txtPhone)
             .keyboardType(.numberPad)
             .modifier(MyTextFieldModifier())
      CustomTextField(tag: 2, placeholder: "Email",strText:self.$txtEmail)
             .keyboardType(.emailAddress)
             .modifier(MyTextFieldModifier())
  • Images图片

TextField 编辑开始

SwiftUI standard .keyboardType is for SwiftUI standard TextField . SwiftUI 标准.keyboardType用于 SwiftUI 标准TextField If you use UIKit UITextField , you have to use corresponding UIKit UIKeyboardType .如果您使用 UIKit UITextField ,则必须使用相应的 UIKit UIKeyboardType

So below is fixed variant (tested with Xcode 11.4 / iOS 13.4) for usage所以下面是使用的固定变体(使用 Xcode 11.4 / iOS 13.4 测试)

CustomTextField(tag: 1, placeholder: "Phone", 
    strText:self.$txtPhone, keyboardType: .phonePad)

演示

and code和代码

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM