美文网首页程序员
iOS适配webView键盘被挡住

iOS适配webView键盘被挡住

作者: 心愿2016 | 来源:发表于2020-05-22 10:11 被阅读0次

_dwebview 为webView 并引用IQKeyboardManager库

// 键盘弹起监听 添加通知
-(void)addEventListening
{
    
    
    // 2.监听键盘的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

/**
 *  当键盘改变了frame(位置和尺寸)的时候调用
 
 */
- (void)keyboardWillChangeFrame:(NSNotification *)note
{
    // 设置窗口的颜色
//    self.window.backgroundColor = self.superview.backgroundColor;
    
    // 0.取出键盘动画的时间
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //    CGRect keyboardFrameBeginRect = [[[note userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    // 1.取得键盘最后的frame  UIKeyboardFrameEndUserInfoKey
    CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    // 2.计算控制器的view需要平移的距离
    
    CGFloat transformY = keyboardFrame.origin.y - kScreenHeight;
    
    NSLog(@"高度 %lf ", transformY);
    if (transformY < 0 && iPhoneX) {
        transformY += 34;
    }
    // 3.执行动画
    [self viewAnimateDuration:transformY Duration:duration view:self.view];
    
}

- (void)viewAnimateDuration:(CGFloat)transformY Duration:(CGFloat)duration view:(UIView *)view
{
    
    CGRect rect = _dwebview.frame;
    
    
    rect.origin.y = -transformY;

    rect.size.height = kSCREEN_MAX + transformY;
    _dwebview.frame = rect;
    [UIView animateWithDuration:duration animations:^{
        view.transform = CGAffineTransformMakeTranslation(0, transformY);
    }];
    
}

相关文章

网友评论

    本文标题:iOS适配webView键盘被挡住

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