FocusState SwiftUI not working

Hey! It seems like the focus state is not working. I even tried copying the example from the documentation directly into a new view and it didn't select the different textFields. Anybody that can see anything that I am missing?

struct LoginForm: View {

    enum Field: Hashable {

        case username

        case password

    }



    @State private var username = ""

    @State private var password = ""

    @FocusState private var focusedField: Field?



    var body: some View {

        Form {

            TextField("Username", text: $username)

                .focused($focusedField, equals: .username)



            SecureField("Password", text: $password)

                .focused($focusedField, equals: .password)



            Button("Sign In") {

                if username.isEmpty {

                    focusedField = .username

                } else if password.isEmpty {

                    focusedField = .password

                } else {

                }

            }

        }

    }

}

This is not answer but I'll add in my experience:

I tried adding FocusState to my app earlier today and was not able to get it working the way I wanted. I was using a UUID as a Value instead of a Field enum, and manually setting the @FocusState to anything other than nil was ignored. I then made the FocusState a Bool to test with a single TextField and it worked fine.

I have a similar issue that I identified as being related to the Form. In my case, I had an onSubmit attached to the Form, which basically does the same thing as yours. If I change the Form to a VStack, everything works great, although it loses all of the Form styling, of course.

I've filed a feedback for this: FB9136142

I think this might only not work in previews. I was able to get my code (with the same structure) working as expected in an iOS15 simulator. The preview in Xcode did nothing.

It works for me on very simple views like this, where the button brings the TextEditor into focus as expected.


struct ContentView: View {

    @State var tempField: String = ""

    @State var isShowingSheet: Bool = false

    

    @FocusState var isFocused: Bool

    

    var body: some View {

        VStack {

            TextEditor(text: $tempField)

                .padding()

                .focused($isFocused)

            Button(action: {isFocused = true})

            {

                Text("Focus!")

            }

        }

    }

}

Put them both in a sheet however, and it doesn't work anymore.

struct ContentView: View {

    @State var tempField: String = ""

    @State var isShowingSheet: Bool = false

    

    @FocusState var isFocused: Bool

    

    var body: some View {

        VStack {

            Button(action: {self.isShowingSheet = true}) {

                Text("Sheet!")

            }.sheet(isPresented: $isShowingSheet) {

                TextEditor(text: $tempField)

                    .padding()

                    .focused($isFocused)

                Button(action: {isFocused = true})

                {

                    Text("Focus!")

                }

            }



        }

    }

}

I've also submitted feedback FB9166651.

I’m having a simular issue. I have a form, and @FocusState is also not working for me.

When I read the value of the focusstate before and after setting the focusstate, its null in both cases.

I also thought it was related to the form, but I tried to change it to a VStack, but it was also not working.

Same issue with xcode 13 beta 2. I have my view presented in a sheet, the TextField is inside a Form and .focused($formFocusState) where @FocusState private var formFocusState: Bool does not seem to work at all

The feedback linked above does not seem to exist anymore. Any updates on this issue?

You have to add a large delay for @FocusState to work inside of a sheet. Here is a basic example.

enum FocusableField: Hashable {

    case email

    case password

}



struct ContentView: View {

   @State private var show = false



    var body: some View {

        Button("Show"){

            show.toggle()

        }

        .sheet(isPresented: $show){

            SheetView()

        }

    }

}



struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

            .previewLayout(.sizeThatFits)

    }

}





struct SheetView: View{

    @State private var email = ""

    @State private var password = ""

    @FocusState private var focus: FocusableField?

    

    

    var body: some View{

        NavigationView {

            Form {

                TextField("email", text: $email, prompt: Text("email"))

                    .focused($focus, equals: .email)

                SecureField("password", text: $password, prompt: Text("password"))

                    .focused($focus, equals: .password)

            }

            .navigationTitle("Sign in")

            .onAppear {

                DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {

                    focus = .email

                }

            }

        }

    }

}
FocusState SwiftUI not working
 
 
Q