Tanner
Tanner

Reputation: 183

Make Keyboard Disappear when UITableView Scrolls

I have a UITextField at the top of my Table View, after the text field is selected I want the keyboard to disappear. I know to call [[self view] endEditing:YES]; but I don't know how to check for the scroll. A good example of this is IMessage, when the keyboard is in view you can scroll up to collapse it, I want my table view to work inversely.

Upvotes: 3

Views: 550

Answers (3)

Wain
Wain

Reputation: 119031

UITableView is a subclass of UIScrollView. By being the table delegate (implementing <UITableViewDelegate>) you are also the scroll view delegate (<UIScrollViewDelegate>) and as such you can implement:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

Then you will receive a notification whenever the table view is scrolled. You can then also use scrollView.contentOffset to check where the table has scrolled to (which direction it's scrolling in).

Upvotes: 3

Bob Cromwell
Bob Cromwell

Reputation: 445

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [textField resignFirstResponder];
}

Try other method in UIScrollViewDelegate if you need other behaviour.

Upvotes: 2

Valeriy Van
Valeriy Van

Reputation: 1865

Call resignFirstResponder where appropriate.

It seems you use UITextField in your cells. Use UITextFieldDelegate protocol to know when UITextField ends being edited:

-(void)textFieldDidEndEditing:(UITextField*)textField
{
    [textField resignFirstResponder];
}

Upvotes: 0

Related Questions