Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a return key on a decimal pad in Swift?

I have the decimal pad setup in my app, but how do I dismiss this keyboard? How do I get a done or a return key? I've tried UIReturnKeyType.Done but that didn't show anything.

Something like this

Something like that

like image 593
Amit Kalra Avatar asked Jun 30 '16 21:06

Amit Kalra


2 Answers

Here is a Swift 3 solution using an extension. Ideal if you have several numeric UITextField objects in your app as it gives the flexibility to decide, for each UITextField, whether to perform a custom action when Done or Cancel is tapped.

enter image description here

// //  UITextField+DoneCancelToolbar.swift //  import UIKit  extension UITextField {     func addDoneCancelToolbar(onDone: (target: Any, action: Selector)? = nil, onCancel: (target: Any, action: Selector)? = nil) {              let onCancel = onCancel ?? (target: self, action: #selector(cancelButtonTapped))         let onDone = onDone ?? (target: self, action: #selector(doneButtonTapped))          let toolbar: UIToolbar = UIToolbar()         toolbar.barStyle = .default         toolbar.items = [             UIBarButtonItem(title: "Cancel", style: .plain, target: onCancel.target, action: onCancel.action),             UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil),             UIBarButtonItem(title: "Done", style: .done, target: onDone.target, action: onDone.action)         ]         toolbar.sizeToFit()          self.inputAccessoryView = toolbar     }      // Default actions:       func doneButtonTapped() { self.resignFirstResponder() }     func cancelButtonTapped() { self.resignFirstResponder() } } 

Example of usage using the default actions:

// // MyViewController.swift //  @IBOutlet weak var myNumericTextField: UITextField! {     didSet { myNumericTextField?.addDoneCancelToolbar() } } 

Example of usage using a custom Done action:

// // MyViewController.swift //  @IBOutlet weak var myNumericTextField: UITextField! {     didSet {          myNumericTextField?.addDoneCancelToolbar(onDone: (target: self, action: #selector(doneButtonTappedForMyNumericTextField)))      } }  func doneButtonTappedForMyNumericTextField() {      print("Done");      myNumericTextField.resignFirstResponder()  } 
like image 80
oli Avatar answered Oct 05 '22 08:10

oli


This solution worked for me =)

Source: https://gist.github.com/jplazcano87/8b5d3bc89c3578e45c3e

I made an extension for UITextField and now I can have a "Done Bar" on all the keyboards I want.

extension UITextField{   func addDoneButtonToKeyboard(myAction:Selector?){     let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 300, height: 40))     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: myAction)      var items = [UIBarButtonItem]()     items.append(flexSpace)     items.append(done)      doneToolbar.items = items     doneToolbar.sizeToFit()      self.inputAccessoryView = doneToolbar  } } 

MyViewController

class myViewController:UIViewController,UITextFieldDelegate{    //MARK: - Outlets   @IBOutlet weak var myTextField: UITextField!    //MARK: - Life Cycle   override func viewDidLoad() {       super.viewDidLoad()       myTextField.delegate = self       //You can specify your own selector to be send in "myAction"      myTextField.addDoneButtonToKeyboard(myAction:  #selector(self.myTextField.resignFirstResponder))   } } 

This is the result

like image 23
Karen Gonzalez Avatar answered Oct 05 '22 06:10

Karen Gonzalez