Here using this you can call textFieldSholdReturn method from a button Action :
var textField : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//Fixed space
let fixed = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: self, action: nil)
fixed.width = 10
//Text
let text = UIBarButtonItem(title: "My Title", style: UIBarButtonItemStyle.plain, target: self, action: nil)
text.setTitleTextAttributes([
NSAttributedStringKey.font : UIFont.systemFont(ofSize: 23.0),
NSAttributedStringKey.foregroundColor : UIColor.white], for: UIControlState.normal)
//TextField
textField = UITextField(frame: CGRect(x: 0, y: 0, width: 150, height: 30))
textField.delegate = self
textField.textColor = UIColor.blue
let border = CALayer()
let width : CGFloat = 2.0
border.borderColor = UIColor.white.cgColor
border.frame = CGRect(x: 0, y: textField.frame.size.height-width, width: textField.frame.size.width, height: textField.frame.size.height)
border.borderWidth = width
textField.layer.addSublayer(border)
textField.layer.masksToBounds = true
let textFieldButton = UIBarButtonItem(customView: textField)
//Search Button
let search = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.search, target: self, action: #selector(ViewController.callExplictly(sender:)))
//Flexible Space
let flexible = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: self, action: nil)
//Toolbar
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 20 , width: view.frame.width, height: 50))
toolbar.sizeToFit()
toolbar.barTintColor = UIColor.orange
toolbar.isTranslucent = false
toolbar.tintColor = UIColor.white
toolbar.items = [fixed, text, fixed, textFieldButton, flexible, search]
view.addSubview(toolbar)
}
You need to use a method as selector Not just directly calling Delgate method of textfield
-- Adding Selector to a UIBarButton :
//Search Button
let search = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.search, target: self, action: #selector(ViewController.callExplictly(sender:)))
Here required Method to call the delegate Method
@objc func callExplictly(sender: UIBarButtonItem){
print("its called")
if let textField = textField, let textFieldDelegate = textField.delegate {
if textFieldDelegate.textFieldShouldReturn!(textField) {
textField.endEditing(true)
}
}
//here let textField --- Here I am just using. reference to TextField used = textField --- this is textField declared and being used in Bar
//I am calling textField.endEditing(true) to dismiss opened Keyboard as Done button refer that My action required on TF is completed now I can Dismiss keyboard too.
// As here textFieldDelegate.textFieldShouldReturn! -- (!) this symbolises as textFieldShouldReturn is a delegate method of TextField and I am accessing that using a reference which is not of textField Type So I am using ! as optional so, it dont jump over the function and call it
}
func textFieldDidBeginEditing(_ textField: UITextField) {
view.backgroundColor = UIColor.yellow
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.backgroundColor = UIColor.cyan
//Hide the keyboard
textField.resignFirstResponder()
}
Here is Delegate method which is called
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
view.backgroundColor = UIColor.white
print("Called")
//Hide the keyboard
textField.resignFirstResponder()
return true
}
Also check my code file for your reference :
Link : https://drive.google.com/open?id=0Bz8kF1Gedr7fcTdEX29WX1JvYlU