ios 中键盘被遮挡解决方案

1.当view是非可以滚动的view时,

// 添加对键盘的通知
- -(void)viewDidLoad{
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- -(void)keyboardWillShow:(NSNotification *)sender{
{
    
    // 得到键盘的高度
    NSDictionary *dict = [sender userInfo];
    CGSize keyboardSize = [dict[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue].size;
    
    
    // 得到输入框CGRectGetMaxY(<#CGRect rect#>)+40到底部的高度
    CGFloat height = (SCREEN_H - (_IPText.frame.origin.y + _IPText.frame.size.height + 40));
    CGFloat newY = -keyboardSize.height + height;
    
    // 如果键盘的高度大于上面的高度
    if (keyboardSize.height > height) {
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.2];
        self.view.frame = CGRectMake(0, newY, SCREEN_W, SCREEN_H);
        [UIView commitAnimations];
        
    }
} - (void)keyboardWillHide:(NSNotification *)sender { [UIView beginAnimations:nil context:NULL]; // 动画时间3s [UIView setAnimationDuration:0.2]; self.view.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H); [UIView commitAnimations]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [_IPText resignFirstResponder]; }

2.类似于聊天框被遮挡。

- (void)viewDidLoad
{
    // 监听键盘的弹出与隐藏
    // 利用消息中心,首先监听UIKeyboardWillChangeFrameNotification 键盘frame改变的消息
        [[NSNotificationCenter defaultCenter] addObserver:self         selector:@selector(keyboardChangeFrame:)     name:UIKeyboardWillChangeFrameNotification object:nil];
}
//监听键盘弹出与退出的方法
-(void)keyboardChangeFrame:(NSNotification *)sender
{
    /*
     UIKeyboardAnimationCurveUserInfoKey = 7;
     UIKeyboardAnimationDurationUserInfoKey = "0.25";
     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 216}}";
     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 775}";
     UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 559}";
     
     //弹出键盘
     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 216}}";
     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 451}, {375, 216}}";
     
     //隐藏键盘
     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 451}, {375, 216}}";
     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 667}, {375, 216}}";
     */
    
    //transform是相对位移
    
    // 设置窗口的颜色
    self.view.window.backgroundColor = self.tableView.backgroundColor;
    
    // 0.取出键盘动画的时间
    CGFloat duration = [sender.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    // 1.取得键盘最后的frame
    CGRect keyboardFrame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    // 2.计算控制器的view需要平移的距离
    CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height;
    
    // 3.让UITableView的最后一个cell滚到键盘最上面
    // self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
    NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:self.messagesFrame.count - 1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    
    // 4.执行动画(self.view.transform是tableView加上textFiled一起向上移动,也就是整个view)
    [UIView animateWithDuration:duration animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
    }];
    
    //    self.view.transform = CGAffineTransformMakeTranslation(0, -216);
    
}

  

posted on 2016-04-21 17:14  村里竹竿  阅读(435)  评论(0编辑  收藏  举报