在macOS上的SwiftUI中的列表内的TextField:编辑工作不正常 [英] TextField inside a List in SwiftUI on macOS: Editing not working well
问题描述
此问题与使用Xcode 12.4(SwiftUI 2)的 macOS 应用(不是iOS或催化剂)的SwiftUI有关.问题在于,对列表内的TextField的编辑不能很好地进行,实际上,它的效果非常差,以至于起初我以为根本无法对其进行编辑.
This problem is with SwiftUI for a macOS app (not iOS or catalyst), using Xcode 12.4 (SwiftUI 2). The problem is that editing of a TextField that is inside a List does not work well, in fact, it works so poorly that at first I thought I could not edit it at all.
这是代码,就像一个简单的例子:
This is the code, just as a simple example:
import SwiftUI
struct ContentView: View {
@State var name1 = "Hans"
@State var name2 = "E"
@State var name3 = ""
var body: some View {
List {
TextField("Name 1", text: $name1)
TextField("Name 2", text: $name2)
TextField("Name 3", text: $name3)
}
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}
这就是我尝试编辑字段时遇到的情况:
And this is what I experience, trying to edit a field:
- 对于非空字段,请在现有文本上按一下 :它可以工作(经过一小段但令人讨厌的0.5秒延迟).
- 对于非空字段,只需点击现有文本的 outside (外部):就不会发生任何事情.
- 对于空白字段(名称3"),请在该字段中的任意位置轻按一下:它会起作用(稍有延迟).
- 双击(在任何字段内的任意位置):没有任何反应.
- For a non-empty field, single-tap on the existing text: it works (after a small, but annoying, 0.5 second delay).
- For a non-empty field, single-tap outside the existing text: nothing happens.
- For an empty field ("Name 3"), single-tap anywhere in the field: it works (after a small delay).
- Double-tap anywhere inside any field: nothing happens.
我希望这不是预期的行为.对于仅包含单个字符的字段而言,这尤其成问题,从而给用户带来困难(必须精确点击"E").我在这里做错什么了吗?
I hope this is not the intended behaviour. It is particularly problematic for a field that contains only a single character, making it difficult for the user (must tap exactly on the "E"). Am I doing something wrong here?
我注意到了问题 SwiftUI列表中的可编辑文本字段,该问题有点类似,但是该问题将文本字段报告给了根本不起作用.
I noted the question Editable TextField in SwiftUI List, which is a bit similar, but that question reported the TextField to not work at all.
推荐答案
找到了解决方案.
import SwiftUI
struct ContentView: View {
@State var name1 = "Hans"
@State var name2 = "E"
@State var name3 = ""
var body: some View {
List {}
.overlay(VStack(spacing: 20){
TextField("Name 1", text: $name1)
TextField("Name 2", text: $name2)
TextField("Name 3", text: $name3)
})
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}
这篇关于在macOS上的SwiftUI中的列表内的TextField:编辑工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!