5

I'm having an issue in Xcode 14 beta where as you can see in the images below the keyboard is dismissed after entering some text whereas in iOS 15 the keyboard stays in place which is the behaviour I want.

What I'm doing is in .onSubmit I'm creating a new item and setting it's focus programatically.

iOS 15 (Xcode 13.4.1)

iOS 15

iOS 16 (Xcode 14 beta 3)

iOS 16

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)
        }
    }
}
Peter Warbo
  • 11,136
  • 14
  • 98
  • 193

0 Answers0