When I add a TextField
with @FocusState
to a toolbar, I noticed that setting focus = false
doesn't cause the form to lose focus
If I move the TextField
out of the toolbar setting focus = false
works fine. How can I unfocus the text field when the cancel button is tapped?
Minimal example tested on Xcode Version 26.0 beta 6 (17A5305f):
import SwiftUI
struct ContentView: View {
@State private var text: String = ""
@FocusState private var focus: Bool
var body: some View {
NavigationStack {
List {
Text("Test List")
}
.toolbar {
ToolbarItem(placement: .bottomBar) {
TextField("Test", text: $text)
.padding(.horizontal)
.focused($focus)
}
ToolbarItem(placement: .bottomBar) {
Button(role: .cancel) {
focus = false // THIS DOESN'T WORK!
}
}
}
}
}
}
#Preview {
ContentView()
}```