Adding A Done Button on the Number Pad


I wanted to know if it was possible to add a done button to the number pad in Xcode. Currently, the number pad looks like this:

I want to make the keyboard disappear when the user taps “done”. Any suggestions? I found some posts on Stack Overflow, but they were confusing and I am looking for a simpler method.

Thanks in advance.


This is what I have used in the past.

I checked and updated one of the methods, still seems to work.


//Call it in viewDidLoad

addDoneButtonOnNumpad(textField: <name of text field>)

func addDoneButtonOnNumpad(textField: UITextField) {
        let keypadToolbar: UIToolbar = UIToolbar()
        // add a done button to the numberpad
            UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: textField, action: #selector(UITextField.resignFirstResponder)),
            UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil)
        // add a toolbar with a done button above the number pad
        textField.inputAccessoryView = keypadToolbar

1 Like

Thank you! It worked.