Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 655
Tanner Avatar asked Jul 09 '26 17:07

Tanner


2 Answers

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).

like image 153
Wain Avatar answered Jul 13 '26 21:07

Wain


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

Try other method in UIScrollViewDelegate if you need other behaviour.

like image 43
Bob Cromwell Avatar answered Jul 13 '26 22:07

Bob Cromwell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!