【问题标题】:Textfield is dismissed when setting a new textfield focus in iOS 16在 iOS 16 中设置新的文本字段焦点时,文本字段被解除
【发布时间】:2022-09-29 09:52:59
【问题描述】:

我在 Xcode 14 beta 中遇到问题,正如您在下面的图片中看到的,键盘在输入一些文本后被关闭,而在 iOS 15 中,键盘保持原位,这是我想要的行为。

我正在做的是.onSubmit 我正在创建一个新项目并以编程方式设置它的焦点。

iOS 15 (Xcode 13.4.1)

iOS 16(Xcode 14 测试版 3)

MRE:

enum Focusable: Hashable {
    case none
    case row(id: UUID)
}

extension View {

    func sync<T: Equatable>(_ field1: Binding<T>, _ field2: FocusState<T>.Binding ) -> some View {
        self
            .onChange(of: field1.wrappedValue) {
                field2.wrappedValue = $0
            }
            .onChange(of: field2.wrappedValue) {
                field1.wrappedValue = $0
            }
    }
}

class Store: ObservableObject {
    
    struct Item: Identifiable {
        var id = UUID()
        var name: String
    }
    
    @Published var items = [Item]()
    @Published var focusedItem: Focusable?
    
    func createNewItem() {
        let newItem = Item(name: \"\")
        items.append(newItem)
        focusedItem = .row(id: newItem.id)
    }
}

struct ContentView: View {
    
    @FocusState private var focusedItem: Focusable?
    
    @StateObject var store = Store()
    
    var body: some View {
        NavigationView {
            List {
                ForEach($store.items) { $item in
                    TextField(\"\", text: $item.name)
                        .focused($focusedItem, equals: .row(id: item.id))
                        .onSubmit(store.createNewItem)
                }
            }
            .toolbar {
                ToolbarItem(placement: .confirmationAction) {
                    Button(\"New item\") {
                        store.createNewItem()
                    }
                }
            }
            .sync($store.focusedItem, $focusedItem)
        }
    }
}
  • 需要 MRE 进行调查
  • @Asperi 我添加了一个 MRE。
  • 这可能是由于在stackoverflow.com/a/73112743/12299030 中报告和调查的相同错误(是的,我认为这是一个错误)。尝试相同的方法。
  • 键盘仍然用这种方法上下跳跃:(
  • 然后只有 Apple 可以提供帮助 - 提交错误。

标签: swiftui keyboard textfield ios16


【解决方案1】:

有没有人找到这个问题的解决方案?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多