是否可以为设置最大长度TextField?我当时正在考虑使用onEditingChanged事件来处理它,但仅在用户开始/完成编辑时才调用它,而在用户键入时不调用它。我也阅读了文档,但还没有找到任何东西。有什么解决方法吗?
TextField($text, placeholder: Text("Username"), onEditingChanged: { _ in
print(self.$text)
}) {
print("Finished editing")
}
Run Code Online (Sandbox Code Playgroud) 我的问题:我希望用户能够从 Textfield 转到 TextField,而视图不会弹起,如下面的 gif 所示。
我的用例:我在多个子视图中有多个文本字段和文本编辑器。这些 TextField 是动态生成的,因此我希望 FocusState 成为一个单独的问题。
我在下面制作了一个 gif 示例和代码示例。
请检查一下,任何建议表示赞赏。
正如评论中所建议的,我做了一些更改,但对弹跳没有影响:
- Using Identfiable does not change the bounce
- A single observed object or multiple and a view model does not change the bounce
Run Code Online (Sandbox Code Playgroud)
我认为这是来自状态更改刷新。如果不是刷新导致跳出(正如用户在评论中建议的那样),那么是什么?使用 FocusState 时有没有办法阻止这种反弹?
重现:创建一个新的 iOS 应用程序 xcode 项目,并将内容视图替换为下面的代码正文。当用户从一个文本字段转到下一个文本字段时,它似乎会刷新视图,导致整个屏幕跳动。
代码示例
import SwiftUI
struct MyObject: Identifiable, Equatable {
var id: String
public var value: String
init(name: String, value: String) {
self.id = name
self.value = value
}
}
struct …Run Code Online (Sandbox Code Playgroud) 我有 2 个带有 NumberPad 键盘类型的 textFields
@IBOutlet weak var ourTextField: UITextField!
@IBOutlet weak var forThemTextField: UITextField!
Run Code Online (Sandbox Code Playgroud)
在 ourTextField 中输入两个数字后,我想自动移动到另一个文本字段(从 ourTextField 到 forThemTextField),然后在转到另一个文本字段(forThemTextField)并输入 2 个数字后,我希望键盘自动隐藏
我通过以下代码添加了一个条件,只接受我的 textFields 中的两个数字:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let lengthsDictionary = [ourTextField : 2, forThemTextField: 2]
guard let length = lengthsDictionary[textField] else {
return true
}
let currentCharacterCount = textField.text?.count ?? 0
if (range.length + range.location > currentCharacterCount){
return false
}
let newLength = currentCharacterCount + string.count - …Run Code Online (Sandbox Code Playgroud) I'm trying to create an input similar like this (?) with SwiftUI.
I've made progress but I can't figure out how to change the height of the text fields and make the input text size bigger.