[英]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.