Skip to content

Instantly share code, notes, and snippets.

@jplazcano87
Created December 28, 2015 15:13
Show Gist options
  • Save jplazcano87/8b5d3bc89c3578e45c3e to your computer and use it in GitHub Desktop.
Save jplazcano87/8b5d3bc89c3578e45c3e to your computer and use it in GitHub Desktop.
Add Done Button to Numeric pad iOS (Swift)
@IBOutlet weak var txtNumber: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
//--- add UIToolBar on keyboard and Done button on UIToolBar ---//
self.addDoneButtonOnKeyboard()
}
func addDoneButtonOnKeyboard()
{
let doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 50))
doneToolbar.barStyle = UIBarStyle.Default
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let done: UIBarButtonItem = UIBarButtonItem(title: "Aceptar", style: UIBarButtonItemStyle.Done, target: self, action: Selector("doneButtonAction"))
var items = [UIBarButtonItem]()
items.append(flexSpace)
items.append(done)
doneToolbar.items = items
doneToolbar.sizeToFit()
self.txtNumber.inputAccessoryView = doneToolbar
}
func doneButtonAction()
{
self.txtNumber.resignFirstResponder()
}
@reimond
Copy link

reimond commented Feb 7, 2018

gracias señor

@abhi21git
Copy link

abhi21git commented Jun 28, 2020

extension UITextField {
    func addDoneButtonOnKeyBoardWithControl() {
	let keyboardToolbar = UIToolbar(frame: CGRect(0, 0, UIScreen.main.bounds.width, 44))
	keyboardToolbar.sizeToFit()
	keyboardToolbar.barStyle = .default
	let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
	let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.endEditing(_:)))
	keyboardToolbar.items = [flexBarButton, doneBarButton]
	self.inputAccessoryView = keyboardToolbar
    }
}

Then just call this on your textfield.

@famictech2000
Copy link

I am unsure what you mean by call this on your textfield?

@lacrlumston
Copy link

thanks! It's useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment