iOS12 WKWebView出现input 键盘页面上顶不下移解决方法

1.如果是H5中只有一个input 参考 https://juejin.im/post/5bfcbaccf265da615e0526ca

  

/// 监听将要弹起
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow) name:UIKeyboardWillShowNotification object:nil];
/// 监听将要隐藏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHidden) name:UIKeyboardWillHideNotification object:nil];
/** 键盘谈起屏幕偏移量 */
@property (nonatomic, assign) CGPoint keyBoardPoint;

#pragma mark - addObserverKeyboard
/// 键盘将要弹起
- (void)keyBoardShow {
    CGPoint point = self.webView.scrollView.contentOffset;
    self.keyBoardPoint = point;
}
/// 键盘将要隐藏
- (void)keyBoardHidden {
    self.webView.scrollView.contentOffset = self.keyBoardPoint;
}

2.如果有多个input 就会出现很严重的问题,页面会下拉回去的动作,用户体验很不好。尝试了多种方法,找到了一种解决方案。

@property (nonatomic,assign) BOOL keyBoardHide;

        /// 监听将要弹起
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow) name:UIKeyboardWillShowNotification object:nil];
        /// 监听将要隐藏
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHidden:) name:UIKeyboardWillHideNotification object:nil];



#pragma mark - addObserverKeyboard
///// 键盘将要弹起
- (void)keyBoardShow {
    self.keyBoardHide = NO;
}
///// 键盘将要隐藏
- (void)keyBoardHidden:(NSNotification *)notification{
   self.keyBoardHide = YES;
    CGFloat actionDelayTime = 0.04;
    __block double pianYi = self.webView.scrollView.contentOffset.y + DEVICE_HEIGHT - self.webView.scrollView.contentSize.height;
    __block CGPoint ontentOffset = CGPointMake(0, self.webView.scrollView.contentSize.height - DEVICE_HEIGHT);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(actionDelayTime * NSEC_PER_SEC)),dispatch_get_global_queue(0, 0), ^{
        if (self.keyBoardHide) {
            NSDictionary *userInfo = notification.userInfo;
            // 动画的持续时间
            double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
            if (@available(iOS 12.0, *)) {
                  
                  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((duration-actionDelayTime) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                      [UIView animateWithDuration:(duration-actionDelayTime) animations:^{
                          UIScrollView *scrollView = [self.webView scrollView];
                          if (pianYi > 0) {
                              [scrollView setContentOffset:ontentOffset];
                          }
                      }];
                  });

             }
            
        }
    }); 
    
}

 

 

你可能感兴趣的:(Objective-C,Xcode)