Is there a type of keyboard UITextField that looks like a numeric keypad but accepts negative numbers?

I set UIKeyboardType as UIKeyboardTypeNumberPad on my UITextFields. However, this 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 a minus sign (-), and if not, can I create this?

Thanks.

+7
ios objective-c uitextfield uikeyboardtype
source share
2 answers

Annoying as there is no such system keyboard. Besides creating a custom keyboard, the best choice is UIKeyboardTypeNumbersAndPunctuation and performing validation to ensure a valid numeric entry.

+7
source share

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

You need to add, after creating myTextField this code, be 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 to 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]; } } 
+2
source share

All Articles