美文网首页
点击任意区域隐藏键盘

点击任意区域隐藏键盘

作者: 陈大帅 | 来源:发表于2016-04-27 17:30 被阅读59次

#pragma mark - 通过GestureRecognizer实现点击任意区域隐藏键盘

- (void)setKeyBoardAutoHidden{

   NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

   //SingleTap Gesture

   UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapDismissKeyboard:)];

   NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];

   //UIKeyboardWillShowNotification

   [notificationCenter addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {

       [self.view addGestureRecognizer:singleTapGesture];

   }];

   //UIKeyboardWillHideNotification

   [notificationCenter addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {

       [self.view addGestureRecognizer:singleTapGesture];

   }];

}

- (void) backgroundTapDismissKeyboard:(UIGestureRecognizer *) gestureRecognizer{

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

   [self.view endEditing:YES];

}

相关文章

网友评论

      本文标题:点击任意区域隐藏键盘

      本文链接:https://www.haomeiwen.com/subject/ipvcrttx.html