The question asks how to do it with the return key but I think this could help someone with the intent to just make keyboard disappear when using UITextView:
@IBOutlet weak var textView: UITextView! private func addToolBarForTextView() { let textViewToolbar: UIToolbar = UIToolbar() textViewToolbar.barStyle = UIBarStyle.Defaultdefault textViewToolbar.items = [ UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Donedone, target: self, action: #selector(self.cancelInput)), UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpaceflexibleSpace, target: self, action: nil), UIBarButtonItem(title: "Done""Post Reply", style: UIBarButtonItemStyle.Donedone, target: self, action: #selector(self.doneInput)) ] textViewToolbar.sizeToFit() self.textView yourTextView.inputAccessoryView = textViewToolbar //do it for every relevant textView if } @objc therefunc arecancelInput() more{ thanprint("cancel") one } @objc func doneInput() { self.textView.resignFirstResponderprint("done") } override func cancelInputviewDidLoad() { self.textView.text = ""super.viewDidLoad() self.textView.resignFirstResponder addToolBarForTextView() } Call addToolBarForTextView() in the viewDidLoad or some other life cycle method.
It seems that was the perfect solution for me.
Cheers,
Murat