码迷,mamicode.com
首页 > 移动开发 > 详细

iOS点击空白区域隐藏键盘

时间:2017-07-03 17:19:15      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:uiview   tap   set   self   __weak   note   res   board   UI   

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

标签:uiview   tap   set   self   __weak   note   res   board   UI   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!