1.webview页面添加 键盘隐藏和显示的监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification
object:nil];
2.实现监听方法
-(void)keyboardWillHide{
/*这个判断也能解决页面不恢复的问题,但是这个方法会引发另一个问题:当页面有多个输入框的时候,在相互切换时,页面会跟随切换抖动,因为键盘在隐藏和显示的时候页面会有一个上下移动的动画,所以造成了在切换输入框时页面抖动的问题.*/
// if (@available(iOS 12.0, *)) {
// WKWebView *webview = (WKWebView*)self.webV;
// for(UIView* v in webview.subviews){
// if([v isKindOfClass:NSClassFromString(@"WKScrollView")]){
// UIScrollView *scrollView = (UIScrollView*)v;
// [scrollView setContentOffset:CGPointMake(0, 0)];
// }
// }
// }
if (@available(iOS 12.0, *)) {
self.webV.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
}
- (void)keyboardWillShow{
if (@available(iOS 12.0, *)) {
self.webV.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
}
}