Reputation: 1679
I made a customTextFieldClass and also had been added a Modifier
for TextField
in my app. But problem is .keyBoardType
not working in 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"))
}
}
- 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())
Upvotes: 2
Views: 1194
Reputation: 257533
SwiftUI standard .keyboardType
is for SwiftUI standard TextField
. If you use UIKit UITextField
, you have to use corresponding UIKit UIKeyboardType
.
So below is fixed variant (tested with Xcode 11.4 / iOS 13.4) for usage
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
}
}
Upvotes: 2