Anj*_*iya 6 textfield ios swiftui
我正在使用SwiftUI的TextField View
。我主要有2个问题,
1)在Swift中,我们可以像这样从情节提要中将TextField的Return Key(Text Input Traits)设置Next
为TextField吗?为此,在SwiftUI中使用哪个修饰符?
2)我有两个文本字段,当我单击键盘上的返回/下一步按钮时,如何导航到下一个文本字段?
任何人都可以帮助执行此功能或其他任何替代方法吗?
我的问题是关于SwiftUI而不是UIKit :)
任何帮助将非常感激!!
要解决你的两个问题,你需要工作,UIKit的从SwiftUI。首先,您需要使用UIViewRepresentable定制TextField。这是用于测试目的的示例代码,尽管该代码不是那么优雅。我敢打赌,将会有一个更强大的解决方案。
这是示例代码:
import SwiftUI
struct KeyboardTypeView: View {
@State var firstName = ""
@State var lastName = ""
@State var focused: [Bool] = [true, false]
var body: some View {
Form {
Section(header: Text("Your Info")) {
TextFieldTyped(keyboardType: .default, returnVal: .next, tag: 0, text: self.$firstName, isfocusAble: self.$focused)
TextFieldTyped(keyboardType: .default, returnVal: .done, tag: 1, text: self.$lastName, isfocusAble: self.$focused)
Text("Full Name :" + self.firstName + " " + self.lastName)
}
}
}
}
struct TextFieldTyped: UIViewRepresentable {
let keyboardType: UIKeyboardType
let returnVal: UIReturnKeyType
let tag: Int
@Binding var text: String
@Binding var isfocusAble: [Bool]
func makeUIView(context: Context) -> UITextField {
let textField = UITextField(frame: .zero)
textField.keyboardType = self.keyboardType
textField.returnKeyType = self.returnVal
textField.tag = self.tag
textField.delegate = context.coordinator
textField.autocorrectionType = .no
return textField
}
func updateUIView(_ uiView: UITextField, context: Context) {
if isfocusAble[tag] {
uiView.becomeFirstResponder()
} else {
uiView.resignFirstResponder()
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UITextFieldDelegate {
var parent: TextFieldTyped
init(_ textField: TextFieldTyped) {
self.parent = textField
}
func updatefocus(textfield: UITextField) {
textfield.becomeFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if parent.tag == 0 {
parent.isfocusAble = [false, true]
parent.text = textField.text ?? ""
} else if parent.tag == 1 {
parent.isfocusAble = [false, false]
parent.text = textField.text ?? ""
}
return true
}
}
}
Run Code Online (Sandbox Code Playgroud)
macOS 12.0+、\nMac Catalyst 15.0+、\ntvOS 15.0+、\nwatchOS 8.0+
\n\n\n使用
\nsubmitLabel(_:)
视图修饰符设置视图的提交标签。它需要指定的预定义情况SubmitLabel
\n\n使用
\n.next
。它定义了一个提交标签,其文本为\xe2\x80\x9cNext\xe2\x80\x9d。
\n\n用于
\nonFocus(_:)
查找修改后的视图层次结构(在本例中为 )TextField
何时失去焦点。当出现时,将焦点放在下一个视图上 (SecureField
)
struct LoginForm: View {\n enum Field: Hashable {\n case usernameField\n case passwordField\n }\n \n @State private var username = ""\n @State private var password = ""\n @FocusState private var focusedField: Field?\n \n var body: some View {\n Form {\n TextField("Username", text: $username)\n .focused($focusedField, equals: .usernameField)\n .submitLabel(.next)\n .onFocus { isFocused in\n if (!isFocused) {\n focusedField = .passwordField\n }\n }\n \n SecureField("Password", text: $password)\n .focused($focusedField, equals: .passwordField)\n .submitLabel(.done)\n }\n }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n\n
归档时间: |
|
查看次数: |
831 次 |
最近记录: |