首页 > 代码库 > 隐藏键盘 (textfield & textView)

隐藏键盘 (textfield & textView)

// textfield

// 点击背景隐藏键盘

    UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(keyboardHide)];

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

    [self.viewaddGestureRecognizer:tap];

    

    // 点击回车隐藏键盘

    [_textFieldaddTarget:selfaction:@selector(keyboardHide) forControlEvents:UIControlEventEditingDidEndOnExit];

 

// 隐藏键盘

- (void)keyboardHide

{

    [_textField resignFirstResponder];

}

 

 

 

 

 

 

// textView

// 点击背景隐藏键盘

    UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(keyboardHide)];

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

    [self.viewaddGestureRecognizer:tap];

 

#pragma mark - UITextViewDelegate -

// 隐藏键盘

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{

    if ([@"\n"isEqualToString:text] == YES)

    {

        [textView resignFirstResponder];

        

        returnNO;

    }

    

    returnYES;

}

 

// 隐藏键盘

- (void)keyboardHide

{

    [_textView resignFirstResponder];

}

隐藏键盘 (textfield & textView)