[英]Textfield keyboardType is not working in SwiftUI
我制作了一个customTextFieldClass ,并且还在我的应用程序中添加了一个TextField
Modifier
。 但问题是.keyBoardType
在TextField
中不起作用。
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
}
}
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())
SwiftUI 标准.keyboardType
用于 SwiftUI 标准TextField
。 如果您使用 UIKit UITextField
,则必须使用相应的 UIKit UIKeyboardType
。
所以下面是使用的固定变体(使用 Xcode 11.4 / iOS 13.4 测试)
CustomTextField(tag: 1, placeholder: "Phone",
strText:self.$txtPhone, keyboardType: .phonePad)
和代码
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.