16

Right now when the keyboard launches on my application it defaults to the letter side showing an alphabetical keyboard.

Question will be listed below images

Refer to image below:

screenshot

This is good. Clicking 123 will show the number side.

screenshot

The Question:

However, I want to by default show the number side and still be able to switch back to the letter side later WHEN THE KEYBOARD IS OPEN. How do I do this?

Setting the keyboard programmatically to a different type is NOT the answer!

For example: User clicks in the text field, keyboard pops up and defaults to let's say the ABC side. The user can click 123 to switch. This is what I want. If the ability to switch while the keyboard is open is taken away it defeats the point of this question.

So if the Name Phone Pad keyboard defaults to the ABC side initially. I want it to default to the 123 side that way while the keyboard is still open it can switch to ABC again when the user clicks ABC.

I want to do this because I have different settings for how to search for barcodes. Either by name of the product or number based on settings the user set about how to index results. This way if they are sorting by number it suggests numbers first, BUT they can still go back to searching by alphabetical if they wanted by clicking the ABC button on the keyboard.

Here is the current setting for my keyboard.

screenshot

shim
  • 9,289
  • 12
  • 69
  • 108
Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154
  • 1
    Your current Keyboard Type **Numbers and Punctuation** does exactly what you seem to be asking for. Doesn't it? – Pankaj Apr 11 '16 at 08:43
  • no it defaults to ABC side first. I want it to show number side. – Joseph Astrahan Apr 11 '16 at 08:47
  • I recently switched to Name Phone Pad, which is a bit better for my situation, still same issue though. – Joseph Astrahan Apr 11 '16 at 10:00
  • 1
    well, I just tested the **Numbers and Punctuation** Keyboard type and it works exactly the way you want it to be. Are you sure you are not setting the keyboard type to default elsewhere in the code? Try doing this on a new UISearchBar in the interaction designer. – Pankaj Apr 11 '16 at 10:25
  • 1
    When the keyboard first launches it goes into ABC mode, showing the alphabet letters A,B,C,D,E,F,G etc... What I want is for it to default to number pad 1,2,3,4,5...it DOES NOT do this by default. Are you sure its going to the 'number pad' first? – Joseph Astrahan Apr 11 '16 at 11:09
  • okay, I think I understand what happened now. You are correct it does go there first...therefore I need to switch around my question. – Joseph Astrahan Apr 11 '16 at 11:12
  • How do I make it switch to ABC first then? – Joseph Astrahan Apr 11 '16 at 11:12
  • changed my question to reflect name phone pad where numbers do not show up first in this case. – Joseph Astrahan Apr 11 '16 at 11:14
  • Ok updated all the images as well to reflect how the number pad looks for this type of keyboard. In short the question is how to change the UI states for the keyboard really. – Joseph Astrahan Apr 11 '16 at 11:17
  • I have answered your question below. – Pankaj Apr 11 '16 at 12:17
  • Updated my question to be more specific because there is miscommunication on what I'm trying to achieve. – Joseph Astrahan Apr 12 '16 at 09:49
  • I need exactly the same. Did you solve this issue? How to approach to this problem. I could use numbers and punctations, but it will be better to use name phone pad with default Numpad on screen and alternative view of letters keyboard – Michał Ziobro Oct 10 '18 at 09:07
  • 2
    Was never solved yet unfortunately – Joseph Astrahan Oct 10 '18 at 16:08
  • I ended up having to make a custom `ABC` button only. It only matters for the very first keyboard. So I'm showing the NumberPad to start, which has a blank space in the bottom left button, and I add a custom button that says `ABC`. When the user taps the `ABC` button, it switches the keyboard type to `NamePhonePad` and goes to the alphabetic keyboard. But now, when they hit the number button from the `NamePhonePad`, it goes to the number side without the custom `ABC` button, because going to the number pad from the alphabetic keyboard gives you that button. – casperson Apr 01 '19 at 16:47
  • Think you could post your workaround solution here? @casperson – Joseph Astrahan May 12 '20 at 04:39

2 Answers2

1

keyboardType is property for a UITextField.

textField.keyboardType = UIKeyboardType.UIKeyboardTypeNumberPad

and

textField.keyboardType = UIKeyboardType.UIKeyboardTypeDefault

is how you can switch between the two modes programatically. Hope that helps.

Pankaj
  • 346
  • 1
  • 10
  • but I want to make it so they can still click the 'abc' button to switch? – Joseph Astrahan Apr 11 '16 at 13:12
  • all this does is forcefully change the keyboard from one type to another, but then the user will be stuck with that keyboard. This is what makes this question tricky. – Joseph Astrahan Apr 11 '16 at 13:12
  • 1
    In that case you may want to toggle between UIKeyboardTypeNumbersAndPunctuation and UIKeyboardTypeDefault programatically. The user still gets to see the ABC / 123 button to toggle to the other side in both the cases. I hope this answers your question. – Pankaj Apr 12 '16 at 04:19
  • This does not unfortunately do what I want even if I switch programatically. For example, if I want to switch to UIKeyboardType.UIKeyboardTypeNumberPad, there is no option to get back to ABC. The only way for me to programmatically change that is for the user to 'close' the current keyboard and then relaunch another since the NumberPad does not have ABC switch option. – Joseph Astrahan Apr 12 '16 at 09:44
  • The only thing I can think of is maybe I have to make a custom keyboard? Is there no way to access the default keyboards and just have it switch state from ABC to 123 mode. I get we can do this BEFORE the keyboard is open, but I want to be able to switch when the keyboard is OPEN already. – Joseph Astrahan Apr 12 '16 at 09:46
  • Try `textField.reloadInputViews()` after you change the `keyboardType` programatically. – Pankaj Apr 12 '16 at 09:54
  • I updated my question again to be more clear what I'm looking for. Let me know if this makes the question easier to understand. – Joseph Astrahan Apr 12 '16 at 09:55
  • Are you suggesting I make my own ABC/123 buttons to switch type? I could do this and put it above the keyboard I suppose but not sure if that is best solution? In which case textField.reloadInputViews would work. Would be nicer if there was way to talk directly to the keyboard to switch states though. – Joseph Astrahan Apr 12 '16 at 09:57
-1

To switch the Keyboardlayout programmatically you have to mutate UITextfield's keyboardType attribute.

Try

textField.keyboardType = UIKeyboardType.NumberPad;

or

textField.keyboardType = UIKeyboardType.PhonePad;
Erle
  • 173
  • 13
  • All this does though is change the keyboard and force it to that type. I want it so the user can still click ABC to get back to normal. – Joseph Astrahan Apr 11 '16 at 13:15
  • You might want to look at this question http://stackoverflow.com/questions/28338981/how-to-add-done-button-to-numpad-in-ios-8-using-swift and change the button's label and action to fit your requirement. – Erle Apr 11 '16 at 13:52
  • By the way I think you wrote the same line of code there twice also? (on the 'Try' and 'or' ) – Joseph Astrahan Apr 11 '16 at 21:57
  • Oh Right, the seconds should be UIKeyboardType.PhonePad – Erle Apr 12 '16 at 11:40