In my app i want hide keyboard when i start scrolling UITableView. I search about this in internet, and most answer is subclassing UITableView (http://stackoverflow.com/questions/3499810/tapping-a-uiscrollview-to-hide-the-keyboard).
I made subclass but it's dont work.
#import <UIKit/UIKit.h>
@protocol MyUITableViewDelegate <NSObject>
@optional
- (void)myUITableViewTouchesBegan;
@end
@interface MyUITableView : UITableView <UITableViewDelegate, UIScrollViewDelegate> {
id<MyUITableViewDelegate> delegate;
}
@end
.m file
#import "MyUITableView.h"
@implementation MyUITableView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"delegate scrollView"); //this is dont'work
[super scrollViewDidScroll:scrollView];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"delegate myUITableViewTouchesBegan"); // work only here
[delegate myUITableViewTouchesBegan];
[super touchesBegan:touches withEvent:event];
}
- (void)dealloc {
...
I use this class like this. But delegate function myUITableViewTouchesBegan don't work in ViewController
.h
#import <UIKit/UIKit.h>
#import "MyUITableView.h"
@interface FirstViewController : UIViewController <UITableViewDelegate, UISearchBarDelegate, MyUITableViewDelegate> {
MyUITableView *myTableView;
UISearchBar *searchBar;
}
@property(nonatomic,retain) IBOutlet MyUITableView *myTableView;
...
.m
- (void) myUITableViewTouchesBegan{
NSLog(@"myUITableViewTouchesBegan");
[searchBar resignFirstResponder];
}
I have some troubles with this implemenation:
1) myUITableViewTouchesBegan dont work in ViewController
2) NSLog from MyUITableView.m - NSLog(@"delegate myUITableViewTouchesBegan"); work only when i touch table. How made it's work also when i start scrolling?
I try override scrollViewDidScroll but comiler said that MyUITableVIew may be don't respond on this string [super scrollViewDidScroll:scrollView];
Not sure why you need to subclass UITableView for this.
In the view controller that contains the plain UITableView, try adding this:
Working solution without writing single line of code in your Controller:
As your question is to handle the hide keyboard with one condition only (on scroll). But here I am recommending one solution to handle textfield and keyboard together which works like charm for UIViewController, UITableView and UIScrollView. The interesting fact is that You do not need to write any single line of code.
Here you go: TPKeyboardAvoiding - An awesome solution to handle keyboard and scroll
Here is the cleanest way to achieve this in iOS 7.0 and above:
Or to dismiss when touch:
Or in Swift:
Or to dismiss interactively when scrolling:
You can do this right in Interface Builder. Select your
UITableView
and open the Attributes Inspector. In the Scroll View section set the Keyboard field to Dismiss on Drag.Task
Hide keyboard programmatically when scroll UITableView in Swift 3
Details
xCode 8.2.1, swift 3
Solution
Full Sample
Result
Just to add an update to the answers above. The below worked for me in Swift 1.2
or