Is there a UITextField keyboard type that like a number pad but allows negative numbers?

12.7k views Asked by At

I am setting the UIKeyboardType as UIKeyboardTypeNumberPad on my UITextFields. However, this only allows the user to enter positive integers. I need the user to be able to enter negative or positive integers. Is there a UIKeyboardType that is identical to UIKeyboardTypeNumberPad but also includes the "-" (minus) symbol, and if not is there a way I can create this?

Thanks.

3

There are 3 answers

6
Clafou On BEST ANSWER

Annoying as it is, there is no such system keyboard available. Other than creating a custom keyboard your best bet is UIKeyboardTypeNumbersAndPunctuation, and doing validation to enforce valid numeric entry.

5
Onik IV On

Add a minus as accesoryView, you need have your textField as a Property, in this example this property is: myTextField;

You need to add, after create your myTextField this code, b.e. in viewDidLoad:

      UIView *inputAccesoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
// It´s good idea a view under the button in order to change the color...more custom option
inputAccesoryView.backgroundColor = [UIColor whiteColor];

UIButton *minusButton = [[UIButton alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width-150)/2, 5, 150, 30)];
// configure the button here... you choose.
[minusButton setTitle:@"-" forState:UIControlStateNormal];
[minusButton addTarget:self action:@selector(changeNumberSing) forControlEvents:UIControlEventTouchUpInside];
[inputAccesoryView addSubview:minusButton];

self.myTextField.inputAccessoryView = inputAccesoryView;

And add the methods of this button in your viewController:

-(void)changeNumberSing
{
if ([self.myTextField.text hasPrefix:@"-"])
{
    self.myTextField.text = [self.myTextField.text substringFromIndex:1];
}else
{
    self.myTextField.text = [NSString stringWithFormat:@"-%@",self.myTextField.text];
}
}
0
Locutus On

Here's a version in Swift that has a +/- button and also a Clear and Done button for the numeric keyboard.

numeric keyboard screenshot

extension UITextField {

func addNumericAccessory(addPlusMinus: Bool) {
    let numberToolbar = UIToolbar()
    numberToolbar.barStyle = UIBarStyle.default

    var accessories : [UIBarButtonItem] = []

    if addPlusMinus {
        accessories.append(UIBarButtonItem(title: "+/-", style: UIBarButtonItem.Style.plain, target: self, action: #selector(plusMinusPressed)))
        accessories.append(UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil))   //add padding after
    }

    accessories.append(UIBarButtonItem(title: "Clear", style: UIBarButtonItem.Style.plain, target: self, action: #selector(numberPadClear)))
    accessories.append(UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil))   //add padding space
    accessories.append(UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(numberPadDone)))

    numberToolbar.items = accessories
    numberToolbar.sizeToFit()

    inputAccessoryView = numberToolbar
}

@objc func numberPadDone() {
    self.resignFirstResponder()
}

@objc func numberPadClear() {
    self.text = ""
}

@objc func plusMinusPressed() {
    guard let currentText = self.text else {
        return
    }
    if currentText.hasPrefix("-") {
        let offsetIndex = currentText.index(currentText.startIndex, offsetBy: 1)
        let substring = currentText[offsetIndex...]  //remove first character
        self.text = String(substring)
    }
    else {
        self.text = "-" + currentText
    }
}

}