Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dismiss keyboard with a uiTextView

I am sure this is not that difficult, but I am having trouble finding info on how to dismiss a keyboard with the return/done key using a textview, not a textfield. here is what I have tried so far(which works with a textfield.)

Thanks very much in advance for any help!

//  PostTravelQuestion.swift  class PostTravelQuestion: UIViewController, UITextViewDelegate {      @IBAction func closepostpage(sender: AnyObject) {         dismissViewControllerAnimated(true, completion: nil)     }       @IBOutlet var postquestion: UITextView!       override func viewDidLoad() {         super.viewDidLoad()          // Do any additional setup after loading the view.         postquestion.delegate = self     }      self addDoneToolBarToKeyboard:self.textView       /*func textViewShouldEndEditing(textView: UITextView) -> Bool {          textView.resignFirstResponder()          return true     }*/      /*override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {         postquestion.resignFirstResponder()         self.view.endEditing(true)     }*/        override func didReceiveMemoryWarning() {         super.didReceiveMemoryWarning()         // Dispose of any resources that can be recreated.     }       func textViewShouldReturn(textView: UITextView!) -> Bool {         self.view.endEditing(true);         return true;     } } 
like image 278
user3708224 Avatar asked Oct 28 '14 03:10

user3708224


People also ask

How do you dismiss a keyboard?

Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.

How to dismiss the keyboard in swift?

Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.

Which delegate function removes keyboard in UITextField?

If you have a UITextField and you want to hide the keyboard when the user is pressing the return key, use the textFieldShouldReturn function. Be sure you have set the textField. delegate = self in your viewDidLoad() function.


1 Answers

This works for me:

import UIKit  class ViewController: UIViewController, UITextViewDelegate {       @IBOutlet weak var textView: UITextView!      override func viewDidLoad() {         super.viewDidLoad()          textView.delegate = self     }      /* Updated for Swift 4 */     func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {         if(text == "\n") {             textView.resignFirstResponder()             return false         }         return true     }      /* Older versions of Swift */     func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {         if(text == "\n") {             textView.resignFirstResponder()             return false         }         return true     }  } 
like image 108
Steve Rosenberg Avatar answered Sep 20 '22 10:09

Steve Rosenberg