Dismiss Keyboard when touch began on UITableView and GoogleMap's View

大城市里の小女人 提交于 2019-12-24 14:33:59

问题


My project design:

UINavigationController ---> UITableViewController ---> UIViewController

I've a UISeachBar on navigationItem.titleView

I tried to use UITapGestureRecognizer, it works but not as expected. Keyboard only dismisses when I touch on an UITableViewCell.

I want UITableView responds to touchesBegan so that I could make the keyboard disappears whenever I touch on an UITableViewCell or just scroll on the table.

Besides that, my UIViewController contains a googlemap's View, it does respond to touchesBegan but only one first touch, after that, every other touches would be ignored.

Can anyone help, pls ?

too long, didn't read: i want to dismiss the virtual keyboard on UITableView likes safari or chrome: when the touch began, not when it ended


回答1:


Try creating a subclass of UITableView like so:

subclassTB.h

 #import <UIKit/UIKit.h>

@interface subClassTB : UITableView

@end


subclassTB.m
#import "subclassTB.h"


 @implementation subClassTB

 - (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    // do your stuff here 
    //NSLog(@"tap registered");
    [super touchesBegan:touches withEvent:event];
}
@end

and in your current controller initialize the table by using

subclassTB *yourtable = [[subclassTB alloc]initWithFrame:youFrame style:yourStyle];




回答2:


Try this

[self.view endEditing:YES];



回答3:


Try this...

Add an empty imageview covering whole view(size of device), then add the gesture recognizer for that imageview and then in the call back method write the below code .

-(void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIImageView *image;//your IBOutlet imageview
    UITapGestureRecognizer *recog=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(called:)];
    [image addGestureRecognizer:recog];
}    

-(void)called:(UITapGestureRecognizer *)sender
{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}


来源:https://stackoverflow.com/questions/20347284/dismiss-keyboard-when-touch-began-on-uitableview-and-googlemaps-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!