ios点击键盘以外空白区域隐藏键盘

在view上添加一个UITapGestureRecognizer
- (void)viewDidLoad
{
    [super viewDidLoad];
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGr.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGr];
}
-(void)viewTapped:(UITapGestureRecognizer*)tapGr{
    [activitySearchBar resignFirstResponder];
}

你可能感兴趣的:(ios点击键盘以外空白区域隐藏键盘)