Changing focus state in onSubmit causes keyboard to bounce

Is there any way to prevent the keyboard from bouncing when changing the focus state in onSubmit? Or is it not recommended to change focus in onSubmit?


The following view is setup so that pressing return on the keyboard should cause focus to move between the TextFields.

struct TextFieldFocusState: View {
    enum Field {
        case field1
        case field2
    }
    
    @FocusState var focusedField: Field?
    
    var body: some View {
        Form {
            TextField("Field 1", text: .constant(""))
                .focused($focusedField, equals: .field1)
                .onSubmit { focusedField = .field2 }
            
            TextField("Field 2", text: .constant(""))
                .focused($focusedField, equals: .field2)
                .onSubmit { focusedField = .field1 }
        }
    }
}

I would expect that when pressing return, the keyboard would say on screen.

What actually happens is the keyboard appears to bounce when the return key is pressed (first half of gif). I assume this is because onSubmit starts dismissing the keyboard then setting the focus state causes the keyboard to be presented again.

The issue doesn't occur when tapping directly on the text fields to change focus (second half of gif).

Yea, this is a bug with iOS since at least 16 I believe. Even Apple's own Code Sample here has this issue:

https://developer.apple.com/documentation/swiftui/focus-cookbook-sample

(To repro: tap the grocery list icon in upper right and you can see this happening when pressing return on the keyboard)

Changing focus state in onSubmit causes keyboard to bounce
 
 
Q