如何在iOS中添加“完成”button到数字键盘

所以,键盘键盘默认没有“完成”或“下一步”button,所以我想添加一个。 在iOS 6和更低版本中,有一些技巧可以在键盘上添加一个button,但是它们在iOS 7中似乎不起作用。

首先我订阅显示通知的键盘

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 

然后,我尝试在键盘显示时添加一个button:

 - (void)keyboardWillShow:(NSNotification *)note { // create custom button UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeSystem]; doneButton.frame = CGRectMake(0, 50, 106, 53); doneButton.adjustsImageWhenHighlighted = NO; [doneButton setTitle:@"Done" forState:UIControlStateNormal]; [doneButton addTarget:self action:@selector(dismissKeyboard) forControlEvents:UIControlEventTouchUpInside]; // locate keyboard view UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; UIView* keyboard; for(int i=0; i<[tempWindow.subviews count]; i++) { keyboard = [tempWindow.subviews objectAtIndex:i]; // keyboard view found; add the custom button to it if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES) [keyboard addSubview:doneButton]; } } 

但for循环无法运行,因为它没有find任何子视图。 有什么build议么? 我找不到任何iOS7的解决scheme,所以有一种不同的方式,我应该这样做?

编辑:感谢所有的工具栏的人的build议,但我宁愿不沿着这条路线,因为我相当空间穷人(这是一种丑陋)。

这是在iOS7 num-keypad中投影完成button的简单方法。 在下面的UITextField的委托方法中,添加一个键盘显示通知。

 -(void)textFieldDidBeginEditing:(UITextField *)textField { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } 

现在执行keyboardWillShow方法如下。 在这里我们需要特别注意iOS7。

 - (void)keyboardWillShow:(NSNotification *)note { // create custom button UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; doneButton.frame = CGRectMake(0, 163, 106, 53); doneButton.adjustsImageWhenHighlighted = NO; [doneButton setImage:[UIImage imageNamed:@"doneButtonNormal.png"] forState:UIControlStateNormal]; [doneButton setImage:[UIImage imageNamed:@"doneButtonPressed.png"] forState:UIControlStateHighlighted]; [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside]; if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { dispatch_async(dispatch_get_main_queue(), ^{ UIView *keyboardView = [[[[[UIApplication sharedApplication] windows] lastObject] subviews] firstObject]; [doneButton setFrame:CGRectMake(0, keyboardView.frame.size.height - 53, 106, 53)]; [keyboardView addSubview:doneButton]; [keyboardView bringSubviewToFront:doneButton]; [UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]-.02 delay:.0 options:[[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue] animations:^{ self.view.frame = CGRectOffset(self.view.frame, 0, 0); } completion:nil]; }); }else { // locate keyboard view dispatch_async(dispatch_get_main_queue(), ^{ UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; UIView* keyboard; for(int i=0; i<[tempWindow.subviews count]; i++) { keyboard = [tempWindow.subviews objectAtIndex:i]; // keyboard view found; add the custom button to it if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES) [keyboard addSubview:doneButton]; } }); } } 

现在将此macros添加到适当的标题来检测SYSTEM_VERSION

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

更安全的方法是使用具有Donebutton的inputAccessoryView作为inputAccessoryView


示例代码:

 UIToolbar *keyboardDoneButtonView = [[UIToolbar alloc] init]; [keyboardDoneButtonView sizeToFit]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneClicked:)]; [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]]; txtField.inputAccessoryView = keyboardDoneButtonView; 

您的-doneClicked方法应该如下所示:

 - (IBAction)doneClicked:(id)sender { NSLog(@"Done Clicked."); [self.view endEditing:YES]; } 

示例代码Swift:

 let keyboardDoneButtonView = UIToolbar.init() keyboardDoneButtonView.sizeToFit() let doneButton = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("doneClicked:"))) keyboardDoneButtonView.items = [doneButton] textFieldInput.inputAccessoryView = keyboardDoneButtonView 

您的-doneClicked方法应该如下所示:

 func doneClicked(sender: AnyObject) { self.view.endEditing(true) } 

更简单的方法:

Swift 3.0及以上版本

 func addDoneButton() { let keyboardToolbar = UIToolbar() keyboardToolbar.sizeToFit() let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done, target: view, action: #selector(UIView.endEditing(_:))) keyboardToolbar.items = [flexBarButton, doneBarButton] textField.inputAccessoryView = keyboardToolbar } 

Swift 2.3及以下版本

 func addDoneButton() { let keyboardToolbar = UIToolbar() keyboardToolbar.sizeToFit() let flexBarButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil) let doneBarButton = UIBarButtonItem(barButtonSystemItem: .Done, target: view, action: #selector(UIView.endEditing(_:))) keyboardToolbar.items = [flexBarButton, doneBarButton] textField.inputAccessoryView = keyboardToolbar } 

目标C

 - (void)addDoneButton { UIToolbar* keyboardToolbar = [[UIToolbar alloc] init]; [keyboardToolbar sizeToFit]; UIBarButtonItem *flexBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self.view action:@selector(endEditing:)]; keyboardToolbar.items = @[flexBarButton, doneBarButton]; self.textField.inputAccessoryView = keyboardToolbar; } 

编辑:

我创build了一个名为DCKit的有用库,它已经有了一个开箱即用的工具栏:

在iOS的键盘上方完成工具栏(使用DCKit库)

它还有许多其他很酷的function。

只是build立在上面的答案与Swift版本,因为我不得不翻译它:

  @IBOutlet weak var numberTextField: UITextField! override func viewDidLoad() { addDoneButtonTo(numberTextField) } // MARK: Done for numberTextField private func addDoneButtonTo(textField: UITextField) { let flexBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) let doneBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "didTapDone:") let keyboardToolbar = UIToolbar() keyboardToolbar.sizeToFit() keyboardToolbar.items = [flexBarButton, doneBarButton] textField.inputAccessoryView = keyboardToolbar } func didTapDone(sender: AnyObject?) { numberTextField.endEditing(true) } 

您可以使用

myTextField.inputAccessoryView = _inputView;

input附件视图是一个总是通过键盘的视图,并与[textfield resignFirstResponder]

在input视图上完成并执行文本域的resignfirst响应者。

只是使用

yourTextField.inputAccessoryView

希望你帮忙

 enter code here 1. register the controller to the notification - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // Keyboard events [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } 2. don't forget to remove the controller from the notification centre -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.view endEditing:YES]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } 3. implement keyboard notification handlers - (void)keyboardWillShow:(NSNotification *)notification { // create custom button UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; doneButton.frame = CGRectMake(0, 107, 106, 53); [doneButton setTitle:@"Done" forState:UIControlStateNormal]; [doneButton addTarget:self action:@selector(doneButton:)forControlEvents:UIControlEventTouchUpInside]; // save the reference to the button in order to use it in keyboardWillHide method self.donekeyBoardBtn = doneButton; // to my mind no need to search for subviews UIWindow *windowContainigKeyboard = [[[UIApplication sharedApplication] windows] lastObject]; [windowContainigKeyboard addSubview:self.donekeyBoardBtn]; self.donekeyBoardBtn.frame = CGRectMake(0., CGRectGetHeight(w.frame) - CGRectGetHeight(self.donekeyBoardBtn.frame), CGRectGetWidth(self.donekeyBoardBtn.frame), CGRectGetHeight(self.donekeyBoardBtn.frame)); } - (void)keyboardWillHide:(NSNotification *)notification { [self.donekeyBoardBtn removeFromSuperview]; } 4. implement done button action - (void)doneButton:(id)sender{ // add needed implementation [self.view endEditing:YES]; } 

这里是一个显示与键盘工具栏button的例子。

由于iPad在“数字”键盘上实现了返回键,因此您需要检测您是在手机还是在iPad上

可以在键盘视图中findhasPrefix:@“UIKeyboard”,该button不能作为子视图添加。 这是我的解决scheme: 在这里input链接描述