首页 > 移动平台 > 详细

iOS点击空白区域隐藏键盘

时间:2017-07-03 16:57:30      阅读:256      评论:0      收藏:0      [点我收藏+]

- (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点击空白区域隐藏键盘

原文:http://www.cnblogs.com/Myzking/p/7111815.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!