美文网首页
键盘遮挡

键盘遮挡

作者: 阶梯 | 来源:发表于2017-10-18 22:29 被阅读14次
//注册键盘出现的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeShown:)
                                             name:UIKeyboardWillShowNotification object:nil];
    //注册键盘消失的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                         name:UIKeyboardWillHideNotification object:nil];
    //点击任一一处,键盘消失
    [self setUpForDismissKeyboard];
#pragma mark - 推出键盘屏幕上移
- (void)keyboardWillBeShown:(NSNotification *)notification {
    
    CGSize keyboardSize = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; // 获取键盘的Size
    //self.keyboardHeight =  keyboardSize.height/3.5;
    [UIView animateWithDuration:0.30 animations:^{
        if (KScreenHeight < 667) {
            self.view.frame = CGRectMake(0, -keyboardSize.height/2, KScreenWidth, KScreenWidth);
        } else {
            self.view.frame = CGRectMake(0, -keyboardSize.height/2, KScreenWidth, KScreenHeight);
        }
    }];
    // _keyboardHasShown = YES;
}


- (void)keyboardWillBeHidden:(NSNotification *)notification{
    [UIView animateWithDuration:0.30 animations:^{
        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        // self.iconView.hidden = NO;
    }];
    // _keyboardHasShown = NO;
}



#pragma mark - 点击屏幕任意一处隐藏键盘
- (void)setUpForDismissKeyboard {
    
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    NSOperationQueue *mainQuene = [NSOperationQueue mainQueue];
    __weak typeof(self)weakSelf = self;
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification
                                       object:nil
                                        queue:mainQuene
                                   usingBlock:^(NSNotification *note){
                                       [weakSelf.view addGestureRecognizer:singleTapGR];
                                   }];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification
                                       object:nil
                                        queue:mainQuene
                                   usingBlock:^(NSNotification *note){
                                       [weakSelf.view removeGestureRecognizer:singleTapGR];
                                   }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    
    //此method会将self.view里所有的subview的first responder都resign掉
    [self.view endEditing:YES];
    
}
- (void) viewDidUnload //dealloc
{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];

//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillShowNotification" object:nil];
//
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillHideNotification" object:nil];
}

相关文章

  • 键盘遮挡

  • 键盘遮挡

    链接 : https://forums.xamarin.com/discussion/comment/356942...

  • 键盘遮挡

  • iOS表单键盘遮挡问题

    iOS表单键盘遮挡问题 iOS表单键盘遮挡问题

  • UITableView(键盘遮挡)

    概述 今天要分享的内容是tableView中使用textFiled键盘遮挡问题,正好做了这个就把它写出来了。方法有...

  • 键盘遮挡问题

    不知道大家是否遇见过这种问题,就是在准备输入文字的时候键盘弹出来遮挡住了输入框,导致无法输入.今天给大家讲讲这个问...

  • 键盘遮挡问题

    pods: pod 'IQKeyboardManager' 头文件 pod 'IQKeyboardManager' 搞定

  • 键盘遮挡问题

    总结下之前项目中遇到的键盘遮挡问题,分三种情况: 1.简单的页面,如登录页面等我会用自己封装的KeyBoardMa...

  • 键盘遮挡处理

    键盘弹起遮住输入框这个问题是每个iOS开发者绕不过去的坎,那么到底该如何进行键盘处理?现在有TPKeyboardA...

  • html 键盘遮挡问题

网友评论

      本文标题:键盘遮挡

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