简体   繁体   English

显示键盘时,Form 中的 SwiftUI Picker 会立即弹回

[英]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 示例应用程序中复制了这个问题,并确定了一些让我相信这可能是一个错误的关键发现:

  1. The number of items in the Form prior to the Picker dictates whether this occurs, and differs based on screen size.选择器之前的表单中的项目数决定了是否发生这种情况,并且根据屏幕大小而有所不同。 For iPhone 13, 9+ TextFields before the Picker causes this to happen;对于 iPhone 13,Picker 之前的 9+ TextFields 会导致这种情况发生; for iPhone 13 Pro Max, 12+ TextFields before the Picker causes this to happen.对于 iPhone 13 Pro Max,Picker 之前的 12+ TextFields 会导致这种情况发生。
  2. With the same number of total items in the Form, moving the Picker higher on the Form prevents this from happening.在表单中的项目总数相同的情况下,将选取器在表单上移到更高的位置可以防止这种情况发生。 If the Picker will not be covered by the keyboard if any TextField is selected, the issue does not happen.如果选择任何 TextField 时键盘不会覆盖 Picker,则不会发生此问题。
  3. If the keyboard is not visible when tapping the Picker, the issue does not happen, regardless of the number of items in the Form.如果在点击选取器时键盘不可见,则无论表单中的项目数量如何,问题都不会发生。
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.

相关问题 SwiftUI NavigationLink push in onAppear 使用@ObservableObject 时立即弹出视图 - SwiftUI NavigationLink push in onAppear immediately pops the view when using @ObservableObject SwiftUI:如果在 ForEach 中使用,NavigationLink 会立即弹出 - SwiftUI: NavigationLink pops immediately if used within ForEach SwiftUI 滚轮选择器选择在滚动时跳回第一项 - SwiftUI wheel picker selection skips back to first item when scrolling 导航回视图时更新 SwiftUI 选择器的最小值和最大值 - Update SwiftUI picker minimum and maximum when navigating back to a view SwiftUI NavigationView 立即在 iOS 上自行弹出视图 15 - SwiftUI NavigationView pops view by itself immediately on iOS 15 键盘显示然后在显示MFMessageComposeViewController时立即隐藏自己 - Keyboard shows then immediately hides itself when showing MFMessageComposeViewController 列表中未显示选择器 | SwiftUI - Picker not showing inside List | SwiftUI SwiftUI - 在表单部分中删除 Picker SegmentedPickerStyle 周围的填充 - SwiftUI - Remove padding around Picker SegmentedPickerStyle when in a Form Section SwiftUI 键盘弹出一次输入并不断出现和消失 - SwiftUI Keyboard pops up once for Input and keeps appearing and disappearing 如何在 SwiftUI 中显示键盘时显示完整列表 - How to show complete List when keyboard is showing up in SwiftUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM