2

I've got a strange problem that I've been unable to solve.

I have a text field in a TableViewCell in a TableView. When I tap the cell a keyboard appears. I have the following code to dismiss said keyboard:

-(void) scrollViewDidScroll:(UIScrollView *)scrollView {
    [self findAndResignFirstResonder:self.view];
}

The problem is, when a TextField is below where the top of the keyboard would be, the TableView scrolls so it is visible. This in turn triggers the scrollViewDidScroll method, which then dismisses the keyboard.

JamEngulfer
  • 747
  • 4
  • 11
  • 33

1 Answers1

2

Makes sense -- the delegate method scrollViewDidScroll: is called every time the tableview is scrolled. So if your scrollViewDidScroll: dismisses the keyboard, then when the app automatically adjusts(scrolls) the tableview content offset to account for the keyboard, it dismisses itself.

You're going to want to implement your keyboard dismissing differently. For one solution, make your UITableViewController a UITextFieldDelegate. Then, implement textFieldShouldReturn: in which you can call [textField resignFirstResponder] or [self.view endEditing:YES] (see Managing the Keyboard (Apple) for more details).

I imagine the effect you were looking for was that the keyboard would be dismissed if the user touched anywhere outside of the UITextField or keyboard. For that I would look at implementing touchesBegan: (as described in Techotopia) or with a UITapGestureRecognizer(as described in this post, just be sure to include gestureRecognizer.cancelsTouchesInView = NO; as it suggests a little further down in the page).

Community
  • 1
  • 1
Louis Tur
  • 1,303
  • 10
  • 16