首页 > 代码库 > 004-实现点击任意屏幕上非文本框点移除键盘
004-实现点击任意屏幕上非文本框点移除键盘
实现点击任意屏幕上非文本框点移除键盘
我们在开发的过程中若要实现此功能只要将下面这些代码拷贝到你要实现的项目里面就可以实现此功能.
代码如下:
- (void)viewDidLoad{ [super viewDidLoad]; [self setUpForDismissKeyboard];}- (void)setUpForDismissKeyboard { NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; UITapGestureRecognizer *singleTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAnywhereToDismissKeyboard:)]; NSOperationQueue *mainQuene =[NSOperationQueue mainQueue]; [nc addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQuene usingBlock:^(NSNotification *note){ [self.view addGestureRecognizer:singleTapGR]; }]; [nc addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQuene usingBlock:^(NSNotification *note){ [self.view removeGestureRecognizer:singleTapGR]; }];}- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer { //此method会将self.view里所有的subview的first responder都resign掉 [self.view endEditing:YES];}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。