@mark might be mixing logic and language?
I don't see a way that I can determine whether the button is already in the keyboard toolbar so I can avoid adding another one.
SwiftUI is a declarative language. You tell SwiftUI what you want on the screen. You've not provided any clues via code snips.
(Why?!?) So this is all guesswork.
I am guessing that SwiftUI is doing exactly what you're telling it to do.
- I have a
MyTextField
that adds one button
to the keyboard toolbar.
- When I add
MyTextField
to the view, then add that button
per step 1.
- When I add THREE
MyTextFields
, then naturally add THREE buttons
to the keyboard toolbar.
Somewhere in @twoStraws lessons, he talks about focused functionality. Design a view to do one thing, and do it well.
Without seeing your code, it seems you have MyTextField
doing more than one thing. Consider refactoring MyTextField
. Move the code that adds a keyboard toolbar button into a smaller more focused func
.
- Layout the form.
- Add
MyTextField
#1
- Add
MyTextField
#2
- Add
MyTextField
#3
- (new func) Add dismiss button to keyboard.
Another technique might be to have an @State variable named showDismissButton
in your view struct. If MyTextField has focus, toggle showDismissButton
to true
. Then wire up the button to hide if showDismissButton
is false
.
These are just rapid, random ideas.