iOS收键盘的几种方式

- (void)viewDidLoad {
    //点击textField之外收起键盘
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    tap.cancelsTouchesInView = NO;//防止tap影响subView响应事件
    [self.view addGestureRecognizer:tap];

    //tableView滑动收起键盘
    [self.table.panGestureRecognizer addTarget:self action:@selector(dismissKeyboard)];
}

#pragma mark - 收起键盘
-(void)dismissKeyboard {
    [self.view endEditing:YES];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    //点击键盘上的return收起键盘
    [textField resignFirstResponder];
    return YES;
}

你可能感兴趣的:(iOS收键盘的几种方式)