4

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.

2 Answers 2

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" : "")
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

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.
doesn't work in pure SwiftUI. You have to go for UIViewRepresentable
0

I'm not sure if you found a solution for this but this appears to work in iOS 17.5.

@FocusState private var focusField: Field?

if focusField == .zipcode {
    ToolbarItemGroup(placement: .keyboard) {
        Spacer()
                
        Button(role: .none) {
            focusNextField()
        } label: {
            Text("next")
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.