1

I am building an app that contains a UITableViewController. In the VC, I put some text fields for accepting user input. I use a table view controller because it's easier to move the view up when the keyboard appears. See here for more info.

When I was learning how to hide the keyboard when the user taps somewhere else by watching one of thenewboston's videos, I was told to use

view.endEditing(true)

in the touchesBegan method.

I also found out how to use a swipe gesture recogniser so I could hide the keyboard when the user swipes down.

However, all of these are with normal view controllers. When I do this with a table view controller, nothing works! Both touchesBegan and UISwipeGestureRecognizer didn't work! When I swipe down or tap somewhere, the keyboard just stays there!

Why is this happening and how can I hide the keyboard in a table view controller?

EDIT:

The relevant code are all in a subclass of UITableViewController:

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    view.endEditing(true)
}


@IBAction func swipedDown(sender: UISwipeGestureRecognizer) { // This is connected to a swipe gesture recogniser
    view.endEditing(true)
}

I think that's how you normally do it, right? I tried this in a normal view controller and it worked. It just doesn't work in table view controllers.

6
  • Update your question with relevant code. Commented Jan 17, 2016 at 4:36
  • @maddy edited. I think that's all the relevant code. Commented Jan 17, 2016 at 4:41
  • Are either of those two functions being called? Commented Jan 17, 2016 at 4:42
  • @maddy No, they are not called. This is really weird. Commented Jan 17, 2016 at 4:45
  • Not really. The table view is probably preventing the gestures from working. Commented Jan 17, 2016 at 4:52

4 Answers 4

3

If you want to override -touchesBegan:withEvent: for your UITableView, you will need to subclass UITableView.

touchesBegan:withEvent: is only sent to subclasses of UIView. You are implementing touchesBegan:withEvent: in your controller.So, it won't work...

You can solve this with UIGestureRecognizer like this -

let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
self.view.addGestureRecognizer(tap)

Then implement the handler -

func handleTap(sender: UITapGestureRecognizer? = nil) {
// handling code
 self.view.endEditing(true)
}

Now call handleTap() at relevant position.

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

2 Comments

Where should I put the first code snippet? The view controller?
Yes in controller's viewDidLoad, also check, I have edited some code , as you are using tableViewController replace tableView.addGestureRecognizer(tap) with self.view.addGestureRecognizer(tap)
3

You can add tableView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.onDrag on viewDidLoad() function of your UITableViewController

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.onDrag
}

Comments

1

By default UITableView handles all the gestures which cannot be overridden unless you add gestures on top of the tableView or handle scrollViewWillBeginDragging: delegate.

By using scrollViewWillBeginDragging: you can hide the keyboard. It'll be called only when the user scrolls. Touches and UIGestures won't work.

Comments

0

You should add a gesture recognizer to your tableView.

let dismissTap = UITapGestureRecognizer(target: self, action: "dismissKeyboard:")
dismissTap.numberOfTapsRequired = 1
view.addGestureRecognizer(dismissTap)

and then in your dismissKeyboard selector:

func dismissKeyboard(recognizer: UITapGestureRecognizer) {
     view.endEditing(true)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.