Для выполнения этой задачи я использовал '.inputAccessoryView' с текстовым полем
On viewDidLoad
self.textField.inputAccessoryView = [self accessoryViewForTextField:self.textField];
затем
- (UIView *)accessoryViewForTextField:(UITextField *)textField{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
view.backgroundColor = [UIColor lightGrayColor];
UIButton *minusButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[minusButton setTitle:@"-" forState:UIControlStateNormal];
[doneButton setTitle:NSLocalizedString(@"Done", @"Done") forState:UIControlStateNormal];
minusButton.backgroundColor = [UIColor magentaColor];
doneButton.backgroundColor = [UIColor blueColor];
CGFloat buttonWidth = view.frame.size.width/3;
minusButton.frame = CGRectMake(0, 0, buttonWidth, 44);
doneButton.frame = CGRectMake(view.frame.size.width - buttonWidth, 0, buttonWidth, 44);
[minusButton addTarget:self action:@selector(minusTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[doneButton addTarget:self action:@selector(doneTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:minusButton];
[view addSubview:doneButton];
return view;
}
это добавит пользовательский вид чуть выше клавиатуры как часть его
наконец, чтобы получить «минус»
#pragma mark - IBActions
- (IBAction)minusTouchUpInside:(id)sender
{
NSString *value = self.textField.text;
if (value.length > 0) {
NSString *firstCharacter = [value substringToIndex:1];
if ([firstCharacter isEqualToString:@"-"]){
self.textField.text = [value substringFromIndex:1];
}else{
self.textField.text = [NSString stringWithFormat:@"-%@", value];
}
}
}
- (IBAction)doneTouchUpInside:(id)sender
{
[self.textField resignFirstResponder];
}