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 that
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.
// // 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() }
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)) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With