1

In a SwiftUI List, when switching from a default keyboard to decimalPad keyboard, the List scrolls down unnecessarily and the focused textfield is hidden by keyboard.

struct ListTextField: View {
    @State private var focusedFieldID: Int?
    @FocusState private var focusedField: Int?

    var body: some View {
        ScrollViewReader { proxy in
            List {
                ForEach(0 ..< 12) { index in
                    Group {
                        TextField("Name", text: .constant(""))
                            .id(index * 2)
                            .focused($focusedField, equals: index * 2)

                        TextField("id", value: .constant(index), format: .number)
                            .keyboardType(.decimalPad)
                            .font(.body)
                            .id(index * 2 + 1)
                            .focused($focusedField, equals: index * 2 + 1)
                    }
                }
            }
        }
        
    }
}

The list should maintain its current scroll position when switching focus to the decimalPad TextField, just like it does for default keyboards.

I tried replacing the List with a ScrollView, and while it works fine functionally, I still need the swipe-to-delete and move/reorder actions that List provides.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.