3

Not sure if this is a bug or if I'm doing something wrong but if I use:

.toolbar {
    ToolbarItemGroup(placement: .navigationBarLeading) {
        if isFocused == .zipCode {
            Text("Test")
        }
    }
}

When the field equaling zipCode is focused, my text shows up. However, if I do the same thing but use placement: .keyboard is does not, unless I also provide an additional view like this:

.toolbar {
    ToolbarItemGroup(placement: .keyboard) {
        if isFocused == .zipCode {
            Text("Test")
        }
        Text("Default")
    }
}

If I do that, I get the word Default on every keyboard and then on my zipCode field where the keyboard changes to a numberPad it shows Default Test.

I am trying to add a keyboard toolbar item only to the numberPad keyboard. I tried modifying the conditional to be TextField.keyboardType == .numberPad but you can't do that, nor could I get UIKeyboardType == .numberPad to work.

This form has multiple fields and I only want to add a button if the numberPad keyboard is shown. I do not want to add anything if other keyboard types are shown.

This is for SwiftUI (latest) with iOS 15.

kittonian
  • 1,020
  • 10
  • 22

1 Answers1

1

The .keyboard ToolBar is problematic in general. See: Duplicate toolbar in SwiftUI.
For your question this would work, but I suppose you want buttons...

    .toolbar {
        ToolbarItemGroup(placement: .keyboard) {
            Text(isFocused == .zipCode ? "ZIP" : "")
        }
    }
ChrisR
  • 9,523
  • 1
  • 8
  • 26
  • I can already do that as per my initial description. The issue is that all the keyboards get a toolbar and I do not want that. I only want the toolbar on a numberPad. – kittonian Mar 08 '22 at 22:27
  • doesn't work in pure SwiftUI. You have to go for `UIViewRepresentable` – ChrisR Mar 08 '22 at 22:40