A. Noesk
A. Noesk

Reputation: 53

Dismissing the keyboard in a UIViewController

I'm running this code within a ViewController class that contains UIViewController and SettingsViewDelegate. The View Controller scene contains a text view that has a scroll bar.

 @IBOutlet weak var tvEditor: UITextView!

 override func viewDidLoad() {
    super.viewDidLoad()      

   // Hide keyboard via swipeDownGestureRecognizer.

     let swipeGesture: UISwipeGestureRecognizer = UISwipeGestureRecognizer (target: self, action: "hideKeyboard")

    swipeGesture.cancelsTouchesInView = false

    UISwipeGestureRecognizerDirection.Down

    }

   func hideKeyboard() {
   tvEditor.resignFirstResponder()
}

When I run my project in the iOS Simulator/device, the keyboard does not respond to a downward swipe. I change the orientation of my device to landscape, and then back to portrait, which is when the keyboard disappears. Can someone please tell me where I'm going wrong?

Upvotes: 5

Views: 2151

Answers (1)

Richard
Richard

Reputation: 109

First, Please add swipeGesture to your viewcontroller. And...

func hideKeyboard() {
    self.view.endEditing(true)
}

It'll works fine for your project. Hope it'll help you.

Upvotes: 8

Related Questions