[英]SwiftUI Picker in Form pops back immediately when keyboard is showing
I have a Form in a NavigationView which displays an unknown number of fields for capturing customer input.我在 NavigationView 中有一个表单,它显示了用于捕获客户输入的未知数量的字段。 Some fields are plain TextFields, others are Pickers with multiple selection.
一些字段是纯文本字段,其他字段是具有多项选择的选取器。 If the Picker is far enough down in the Form to be covered by the keyboard when a TextField is focused, then tapping the Picker with the keyboard up prevents the successful transition to the Picker's auto-generated detail view.
如果在 TextField 获得焦点时,Picker 在 Form 中的位置足够低以被键盘覆盖,则在键盘向上时点击 Picker 会阻止成功转换到 Picker 的自动生成的详细信息视图。 The detail view is pushed and immediately popped, which prevents selection.
详细视图被推送并立即弹出,这会阻止选择。
There are many other questions on this site relating to automatic popping of NavigationView/NavigationLinks, but I have replicated this issue in a simple single ContentView sample application and have identified a few key findings that lead me to believe this may be a bug:该站点上还有许多其他与 NavigationView/NavigationLinks 自动弹出有关的问题,但我已经在一个简单的单个 ContentView 示例应用程序中复制了这个问题,并确定了一些让我相信这可能是一个错误的关键发现:
class Model: ObservableObject {
@Published var fields: [Field] = [
Field(id: "1"), Field(id: "2"), Field(id: "3"), Field(id: "4"), Field(id: "5"), Field(id: "6"),
Field(id: "7"), Field(id: "8"), Field(id: "9"), Field(id: "10"), Field(id: "11"), Field(id: "12"),
Field(id: "13", type: .Picker(options: ["A", "B", "C"])),
]
}
struct Field: Identifiable {
enum FieldType {
case Text
case Picker(options: [String])
}
let id: String
let type: FieldType
var value: String
init(id: String, type: FieldType = .Text) {
self.id = id
self.type = type
self.value = ""
}
}
struct ContentView: View {
@StateObject var model = Model()
var body: some View {
NavigationView {
Form {
Section {
ForEach($model.fields) { $field in
switch field.type {
case .Text:
TextField(field.id, text: $field.value)
case .Picker(let options):
Picker("Picker", selection: $field.value) {
ForEach(options, id: \.self) {
Text($0)
}
}
}
}
}
}
}
}
}
I can confirm the issue.我可以确认问题。 And I can add: also a programmatically activated
NavigationLink
pops back when hidden by the keyboard.我可以补充:当被键盘隐藏时,一个以编程方式激活的
NavigationLink
也会弹出。 I'd say its a bug.我会说它是一个错误。
Example based on OPs code, with additional programmatic Navlink:基于 OPs 代码的示例,带有额外的程序化 Navlink:
struct ContentView: View {
@StateObject var model = Model()
@State private var selected = false
var body: some View {
NavigationView {
Form {
Section {
Button("activate hidden link") { selected = true }
ForEach($model.fields) { $field in
switch field.type {
case .Text:
TextField(field.id, text: $field.value)
case .Picker(let options):
Picker("Picker", selection: $field.value) {
ForEach(options, id: \.self) {
Text($0)
}
}
}
}
NavigationLink("Test Link", isActive: $selected) {
Text("Destination")
}
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.