Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add "Done" button to Numpad in iOS using Swift?

Tags:

ios

swift

numpad

It works fine with the default keyboard, but I cant get it working with the numpad.

Any ideas?

like image 237
Dejan Skledar Avatar asked Feb 05 '15 08:02

Dejan Skledar


People also ask

How do I add a Done button to a number keypad in Swift?

addDoneKeyboardButton() — creates the keyboard done button using UIToolbar. Inside the toolbar, we create a UIBarButtonItem. You can name the button however you want, and you can add multiple buttons (depending on your needs). In other words, use this function to customize the toolbar.


1 Answers

As far as I know, you can't add the Done button on the keyboard part; you'd have add a inputAccessoryView to the UITextField or UITextView (if that's what you're using).

Check the documentation for more info.

Edit: Check this question for an example on how to do that.

Edit 2: Similar example in Swift.

Edit 3: Code from edit 2, as link may expire.

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() } 

Swift 4.2

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()          txtMobileNumber.inputAccessoryView = doneToolbar     }      @objc func doneButtonAction(){         txtMobileNumber.resignFirstResponder()     } 
like image 195
nikolovski Avatar answered Oct 02 '22 21:10

nikolovski