Skip to main content
fixed simple syntax errors
Source Link
Fattie
  • 9.8k
  • 76
  • 455
  • 769

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

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 = UIBarStyle.Default textViewToolbar = [ UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Done, target: self, action: #selector(self.cancelInput)), UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil), UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: #selector(self.doneInput)) ] textViewToolbar() self.textView.inputAccessoryView = textViewToolbar //do it for every relevant textView if there are more than one }   func doneInput() {  self.textView.resignFirstResponder()  } func cancelInput() { self.textView.text = "" self.textView.resignFirstResponder() } 

Call addToolBarForTextView() in the viewDidLoad or some other life cycle method.

It seems that was the perfect solution for me.

Cheers,

Murat

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:

private func addToolBarForTextView() {   let textViewToolbar: UIToolbar = UIToolbar()   textViewToolbar.barStyle = .default   textViewToolbar.items = [   UIBarButtonItem(title: "Cancel", style: .done,  target: self, action: #selector(cancelInput)),   UIBarButtonItem(barButtonSystemItem: .flexibleSpace,  target: self, action: nil),   UIBarButtonItem(title: "Post Reply", style: .done,  target: self, action: #selector(doneInput))   ]   textViewToolbar.sizeToFit()  yourTextView.inputAccessoryView = textViewToolbar } @objc func cancelInput() { print("cancel") } @objc func doneInput() { print("done") } override func viewDidLoad() { super.viewDidLoad()  addToolBarForTextView() } 

Call addToolBarForTextView() in the viewDidLoad or some other life cycle method.

It seems that was the perfect solution for me.

Cheers,

Murat

Source Link
Murat Yasar
  • 1.1k
  • 11
  • 24

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 = UIBarStyle.Default textViewToolbar = [ UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Done, target: self, action: #selector(self.cancelInput)), UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil), UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: #selector(self.doneInput)) ] textViewToolbar() self.textView.inputAccessoryView = textViewToolbar //do it for every relevant textView if there are more than one } func doneInput() { self.textView.resignFirstResponder() } func cancelInput() { self.textView.text = "" self.textView.resignFirstResponder() } 

Call addToolBarForTextView() in the viewDidLoad or some other life cycle method.

It seems that was the perfect solution for me.

Cheers,

Murat