Last active
May 10, 2016 04:03
-
-
Save benpackard/c4ae385ee3287a4b053eb3ced6c99e4e to your computer and use it in GitHub Desktop.
UIViewController extension to add Done button to a UITextField
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIViewController { | |
func addDismissingInputAccessoryToTextField(textField: UITextField) { | |
let doneButton = UIBarButtonItem(barButtonSystemItem: .Done, target: view, action: #selector(UIView.endEditing)) | |
let toolbar: UIToolbar = UIToolbar() | |
toolbar.sizeToFit() | |
let spacer = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil) | |
toolbar.setItems([spacer, doneButton], animated: false) | |
textField.inputAccessoryView = toolbar | |
} | |
} | |
//usage inside a UIViewController | |
let textField = UITextField() | |
addDismissingInputAccessoryToTextField(textField) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment