@FocusState doesn't work with UIHostingConfiguration

Hi! I'm experimenting with UIHostingConfiguration. I put a textfield in the cell's coniguration and would like to observe its state. I created a @FocusState variable and set the focused(_:). But I've noticed that @FocusState doesn't work in UIHostingConfiguration. Here' my code:

struct TaskView: View {

    

    @ObservedObject var task: Task

    @FocusState private var isFocused: Bool



    var body: some View {



        HStack(spacing: 10) {

            Image(systemName: (task.isCompleted == true) ? "checkmark.square.fill" : "square")

                .font(Font.system(size: 20, weight: .medium))

                .foregroundColor(.gray)

                .onTapGesture {

                    task.isCompleted = true

                }

            VStack(alignment: .leading, spacing: 3) {

                TextField("Task", text: $task.name)

                    .onSubmit {

                        try! (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext.save()

                    }

                    .focused($isFocused)

                    .submitLabel(.done)

                    .onChange(of: isFocused) { newValue in

                        print(newValue) //always returns false

                    }

                HStack(spacing: 2) {

                    Image(systemName: "moon.fill")

                        .resizable()

                        .frame(width: 10, height: 10)

                        .foregroundColor(.blue)

                    Text("Tomorrow")

                        .font(.footnote).bold()

                        .foregroundColor(.gray)

                }

            }

        }

    }
}

Replies

@FocusState should work with SwiftUI views inside UIHostingConfiguration. If you have a reproducible case where it is not working properly, can you please file a bug report with a small sample project attached so that we can investigate and see why it's not working as expected for you?

  • Should I use the Feedback App to report this bug?

  • I would recommend using http://feedbackassistant.apple.com on your Mac, so you can easily attach a small Xcode sample project.

  • I created a ticket. Thank you for your help.