首页 > 代码库 > 关闭键盘

关闭键盘

1、点击固定的按钮关闭键盘

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    return YES;

}

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

{

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

   {

       [textView resignFirstResponder];

       return YES;

   }

    return YES;

}

2、点击其他关闭键盘

- (void)viewDidLoad

{

    [super viewDidLoad];

    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];

    tapGr.cancelsTouchesInView = NO;

    [self.view addGestureRecognizer:tapGr];

}

-(void)viewTapped:(UITapGestureRecognizer*)tapGr{

    [self.textField1 resignFirstResponder];

}

关闭键盘