Say we have the following view of two text fields:
struct ContentView: View {
@State private var first = ""
@State private var second = ""
var body: some View {
VStack {
TextField("First", text: $first)
.toolbar {
ToolbarItem(placement: .keyboard) {
Button("Test") { }
}
}
TextField("Second", text: $second)
}
}
}
The toolbar
modifier is applied only to the "first" text field. My expectation is therefore that it only shows up on the keyboard, when the "first" text field is in focus.
What happens in practice though, it that it also shows up when the "second" text field is in focus.
Is this intended behaviour? And if so, how can I have different keyboard toolbars for different text fields?
FocusState
andfocused
and how to make conditional modifiers. – StopoverFocusState
andfocused
make no difference here, adding a toolbar to apparently one text field adds it to all of them regardless of focus. – Ensoul