最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swiftui - FocusState Issue in iOS 18 with Keyboard Navigation - Stack Overflow

programmeradmin2浏览0评论

I have implemented a SwiftUI view containing a grid of TextField elements, where focus moves automatically to the next field upon input. This works as it should on iOS 16 and 17, maintaining proper focus highlighting when keyboard full access is enabled.

Below is the simple SwiftUI code for reference:

struct AutoFocusGridTextFieldsView: View {
    private let fieldCount: Int
    private let columns: Int
    @State private var textFields: [String]
    @FocusState private var focusedField: Int?

    init(fieldCount: Int = 17, columns: Int = 5) {
        self.fieldCount = fieldCount
        self.columns = columns
        _textFields = State(initialValue: Array(repeating: "", count: fieldCount))
    }

    var body: some View {
        let rows = (fieldCount / columns) + (fieldCount % columns == 0 ? 0 : 1)

        VStack(spacing: 10) {
            ForEach(0..<rows, id: \.self) { row in
                HStack(spacing: 10) {
                    ForEach(0..<columns, id: \.self) { col in
                        let index = row * columns + col
                        if index < fieldCount {
                            TextField("", text: $textFields[index])
                                .frame(width: 40, height: 40)
                                .multilineTextAlignment(.center)
                                .textFieldStyle(RoundedBorderTextFieldStyle())
                                .focused($focusedField, equals: index)
                                .onChange(of: textFields[index]) { newValue in
                                    if newValue.count > 1 {
                                        textFields[index] = String(newValue.prefix(1))
                                    }
                                    if !textFields[index].isEmpty {
                                        moveToNextField(from: index)
                                    }
                                }
                        }
                    }
                }
            }
        }
        .padding()
        .onAppear {
            focusedField = 0
        }
    }

    private func moveToNextField(from index: Int) {
        if index + 1 < fieldCount {
            focusedField = index + 1
        }
    }
}

Steps to reproduce:
1. Turn on Accessibility features -> Full Keyboard access,
2. Run this code,
3. Insert any character, so the focus should move forward

However, in iOS 18 and above, the keyboard full access focus behaves differently. It always stays behind the actual focus state, causing a mismatch between the visually highlighted field and the active text input. This leads to usability issues, especially for users navigating with an external keyboard.

Has anyone experienced something similar and knows the way to fix it?
I've tried also an UIKit equivalent of the view, and it seems like this issue persists.

发布评论

评论列表(0)

  1. 暂无评论