点击空白处以及返回 return 键隐藏键盘

///返回键盘处理

方法1

此方法只能针对继承自 UIControl的子类适用

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.view endEditing:YES];

}

 

方法2

//添加手势方法监听键盘处理的方法

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide)];

    //设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。

    tapGestureRecognizer.cancelsTouchesInView = NO;

    //将触摸事件添加到当前view

    [self.view addGestureRecognizer:tapGestureRecognizer];

//返回键盘处理的方法

-(void)keyboardHide{

    [self.view endEditing:YES];

}

//返回 text return键隐藏键盘

-(BOOL)textFieldShouldReturn:(UITextField *)textField {

    [self.view endEditing:YES];

        return YES;

}

 

 

//3.另外一种方法(取消文本的第一响应事件)

 

//让键盘隐藏

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [_nameTextField resignFirstResponder];

    [_ageTextField resignFirstResponder];

    [_IDTextField resignFirstResponder];

}

 

你可能感兴趣的:(点击空白处以及返回 return 键隐藏键盘)