Default focus on TextEditor does not work

64 Views Asked by At

I want to make a textEditor have default focus when a view appears in SwiftUI. My approach works for a textField but not a textEditor. I'm not sure why this is.

struct ContentView: View {
    enum FocusedField {
        case firstName, lastName
    }

    @State private var firstName = ""
    @State private var lastName = ""
    @FocusState private var focusedField: FocusedField?

    var body: some View {
        
        TextEditor(text: $firstName)
            .focused($focusedField, equals: .firstName)
            .onAppear {
                focusedField = .firstName
             }
    }
}
0

There are 0 best solutions below