Adding A Done Button on the Number Pad

Hi,

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.

Hi,

This is what I have used in the past.

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

Blessings,
—Mark


//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
        keypadToolbar.items=[
            UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: textField, action: #selector(UITextField.resignFirstResponder)),
            UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil)
        ]
        keypadToolbar.sizeToFit()
        // add a toolbar with a done button above the number pad
        textField.inputAccessoryView = keypadToolbar
    }//addDoneToKeyPad

1 Like

Thank you! It worked.