#pragma mark - 通過GestureRecognizer實現(xiàn)點擊任意區(qū)域隱藏鍵盤
- (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];
}