5

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?

1
  • you have not added this swipeGesture to any view. Commented Sep 16, 2015 at 19:17

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You are welcome! :) When you have another question, please let me know.
I added swipeGesture to my viewcontroller, but didn't change tvEditor.resignFirstResponder() to self.view.endEditing(true). That worked fine for a while. When I did make that change, Xcode gave me - fatal error: unexpectedly found nil while unwrapping an Optional value. I changed the code back to what it was formerly, but I'm still getting the error. Does this have something to do with wrongly configuring the gesture, or is there some other problem with my code? As you might've guessed, I'm a beginner.
Hi I'm sorry late reply.
I'm not sure why you can't use it, it works fine for me. So do you mean there are a build error or runtime error? Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.