如何使用Swift在IOS 8中添加“完成”button到Numpad?

它与默认的键盘工作正常,但我不能让它与数字键盘一起工作。

有任何想法吗?

据我所知,你不能在键盘部分添加完成button; 你必须添加一个inputAccessoryViewUITextFieldUITextView (如果这就是你正在使用的)。

检查文档以获取更多信息 。

编辑 :检查这个问题的例子如何做到这一点。

编辑2 : Swift中的类似示例 。

编辑3 :编辑2中的代码,因为链接可能过期。

 override func viewDidLoad() { super.viewDidLoad() //--- add UIToolBar on keyboard and Done button on UIToolBar ---// self.addDoneButtonOnKeyboard() } //--- *** ---// func addDoneButtonOnKeyboard() { var doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 50)) doneToolbar.barStyle = UIBarStyle.BlackTranslucent var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("doneButtonAction")) var items = NSMutableArray() items.addObject(flexSpace) items.addObject(done) doneToolbar.items = items doneToolbar.sizeToFit() self.textView.inputAccessoryView = doneToolbar self.textField.inputAccessoryView = doneToolbar } func doneButtonAction() { self.textViewDescription.resignFirstResponder() self.textViewDescription.resignFirstResponder() } 

一个Swift-3版本Marko的解决scheme – 为我工作

在我的情况下,我有一个UITextField标识为textfield

 func addDoneButtonOnKeyboard() { let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50)) doneToolbar.barStyle = UIBarStyle.default let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(ViewController.doneButtonAction)) var items = [UIBarButtonItem]() items.append(flexSpace) items.append(done) doneToolbar.items = items doneToolbar.sizeToFit() self.textfield.inputAccessoryView = doneToolbar } func doneButtonAction() { self.textfield.resignFirstResponder() } 

您可以将添加工具栏添加到键盘。 可以使用textfield的InputAccessoryView属性来设置这个工具栏。

下面是我用过的代码

 //Add done button to numeric pad keyboard let toolbarDone = UIToolbar.init() toolbarDone.sizeToFit() let barBtnDone = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: #selector(VerifyCardViewController.doneButton_Clicked(_:))) toolbarDone.items = [barBtnDone] // You can even add cancel button too txtCardDetails3.inputAccessoryView = toolbarDone 

截图

如果你的Donebutton应该只是closures了数字键盘,那么最简单的版本就是像这样调用resignFirstResponder作为select器:

 UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: textField, action: #selector(UITextField.resignFirstResponder)) 

此代码适用于iOS9和Swift 2,我在我的应用程序中使用它:

 func addDoneButtonOnNumpad(textField: UITextField) { let keypadToolbar: UIToolbar = UIToolbar() // add a done button to the numberpad keypadToolbar.items=[ UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: textField, action: #selector(UITextField.resignFirstResponder)), UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil) ] keypadToolbar.sizeToFit() // add a toolbar with a done button above the number pad textField.inputAccessoryView = keypadToolbar } 

Swift 3使用@IBInpectable和扩展的解决scheme。 项目中的每个UITextField都将在Interface Builder的属性检查器中出现一个下拉菜单。

 extension UITextField{ @IBInspectable var doneAccessory: Bool{ get{ return self.doneAccessory } set (hasDone) { if hasDone{ addDoneButtonOnKeyboard() } } } func addDoneButtonOnKeyboard() { let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50)) doneToolbar.barStyle = .default let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction)) let items = [flexSpace, done] doneToolbar.items = items doneToolbar.sizeToFit() self.inputAccessoryView = doneToolbar } func doneButtonAction() { self.resignFirstResponder() } } 

首先让我告诉你一件事,你可以在默认的键盘上添加完成button。 其实今天我刚刚解决了这个问题。 现成的代码,只要把它放在你的.swift类上。 我正在使用Xcode 7并使用iPad Retina(ios 9)进行testing。

无论如何,这里没有更多的谈话是代码。

  //Creating an outlet for the textfield @IBOutlet weak var outletTextFieldTime: UITextField! //Adding the protocol of UITextFieldDelegate class ReservationClass: UIViewController, UITextFieldDelegate { func textFieldShouldBeginEditing(textField: UITextField) -> Bool { //Making A toolbar let keyboardDoneButtonShow = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/17)) //Setting the style for the toolbar keyboardDoneButtonShow.barStyle = UIBarStyle .BlackTranslucent //Making the done button and calling the textFieldShouldReturn native method for hidding the keyboard. let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("textFieldShouldReturn:")) //Calculating the flexible Space. let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) //Setting the color of the button. item.tintColor = UIColor .yellowColor() //Making an object using the button and space for the toolbar let toolbarButton = [flexSpace,doneButton] //Adding the object for toolbar to the toolbar itself keyboardDoneButtonShow.setItems(toolbarButton, animated: false) //Now adding the complete thing against the desired textfield outletTextFieldTime.inputAccessoryView = keyboardDoneButtonShow return true } //Function for hidding the keyboard. func textFieldShouldReturn(textField: UITextField) -> Bool { self.view.endEditing(true) return false } } 

这是给我的解决scheme…

在这里输入图像说明

还有一件事我只是想告诉你,这是我在最近的项目中使用的工作解决scheme。 我已经发布了这个,因为这个问题没有可用的解决scheme。 工作守则和解释承诺。

编辑: –

使它更专业…如果你是上面的代码,那么它也会给你工作的解决scheme,但在这里我正在努力使它更专业。 请注意代码MOD。 我留下了前面引用的未使用的代码。

变化..

  1. 创build一个单独的button对象
  2. 将其设置到工具栏
  3. 减小之前创build的工具栏的大小
  4. 使用FlexSpace和NegativeSpace将自定义button放置在屏幕左侧。

     func textFieldShouldBeginEditing(textField: UITextField) -> Bool { let keyboardDoneButtonShow = UIToolbar(frame: CGRectMake(200,200, self.view.frame.size.width,30)) // self.view.frame.size.height/17 keyboardDoneButtonShow.barStyle = UIBarStyle .BlackTranslucent let button: UIButton = UIButton() button.frame = CGRectMake(0, 0, 65, 20) button.setTitle("Done", forState: UIControlState .Normal) button.addTarget(self, action: Selector("textFieldShouldReturn:"), forControlEvents: UIControlEvents .TouchUpInside) button.backgroundColor = UIColor .clearColor() // let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("textFieldShouldReturn:")) let doneButton: UIBarButtonItem = UIBarButtonItem() doneButton.customView = button let negativeSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil) negativeSpace.width = -10.0 let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) //doneButton.tintColor = UIColor .yellowColor() let toolbarButton = [flexSpace,doneButton,negativeSpace] keyboardDoneButtonShow.setItems(toolbarButton, animated: false) outletTextFieldTime.inputAccessoryView = keyboardDoneButtonShow return true } 

它现在给我以下…而且尝试匹配的图片,并find变化。

在这里输入图像说明

谢谢。

希望这有助于。 对不起,很长的答案。

据马科·尼科洛夫斯基回答

这里是Objective-C的答案:

 - (void)ViewDidLoad { [self addDoneButtonOnKeyboard]; } - (void)addDoneButtonOnKeyboard { UIToolbar *toolBarbutton = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]; toolBarbutton.barStyle = UIBarStyleBlackTranslucent; UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonAction)]; NSMutableArray *items = [[NSMutableArray alloc] init]; [items addObject:barBtnItem]; [items addObject:done]; toolBarbutton.items = items; [toolBarbutton sizeToFit]; self.timeoutTextField.inputAccessoryView = toolBarbutton; } - (void)doneButtonAction { [self.view endEditing:YES]; }