iOS点击空白区域隐藏键盘

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS点击空白区域隐藏键盘相关的知识,希望对你有一定的参考价值。

- (void)setupForDismissKeyboard

{

  NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

  UITapGestureRecognizer *singleTagGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAnyWhereToDismissKeyboard:)];

  __weak UIViewController *weakSelf = self;

  NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];

  [nc addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note){[weakSelf.view addGestureRecognizer:singleTapGR];}];

  [nc addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note){[weakSelf.view removeGestureRecognizer:singleTapGR];}];    

}

 

- (void)tapAnyWhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer

{

  //此method 会将self.view里所有的subview的first responder 都resign掉

  [self.view endEditing:YES];

  [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardWillHideNotification object:nil];

}

以上是关于iOS点击空白区域隐藏键盘的主要内容,如果未能解决你的问题,请参考以下文章

Android 点击空白位置并且隐藏软键盘

IOS隐藏软键盘的几种方式

js控制ios端的input/textarea元素失去焦点时隐藏键盘

IOS下H5的input/textarea元素失去焦点时隐藏键盘

Android 实现点击输入框以外的区域隐藏软键盘

Android实战开发篇 点击EditText以外区域,隐藏键盘,失去焦点