diff --git a/src/ios/CDVWKWebViewEngine.m b/src/ios/CDVWKWebViewEngine.m index 2c1936fc..a3d71fec 100644 --- a/src/ios/CDVWKWebViewEngine.m +++ b/src/ios/CDVWKWebViewEngine.m @@ -126,6 +126,8 @@ @implementation CDVWKWebViewEngine @synthesize engineWebView = _engineWebView; +NSTimer *timer; + - (instancetype)initWithFrame:(CGRect)frame { self = [super init]; @@ -363,6 +365,17 @@ - (void)pluginInitialize selector:@selector(onSocketError:) name:@"socketInUseError" object:nil]; + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(keyboardWillHide) + name:UIKeyboardWillHideNotification object:nil]; + + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(keyboardWillShow) + name:UIKeyboardWillShowNotification object:nil]; + + NSLog(@"Using Ionic WKWebView"); [self addURLObserver]; @@ -448,6 +461,31 @@ - (void)onAppWillEnterForeground:(NSNotification *)notification { } } + +-(void)keyboardWillHide +{ + if (@available(iOS 12.0, *)) { + timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false]; + [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; + } +} + +-(void)keyboardWillShow +{ + if (timer != nil) { + [timer invalidate]; + } +} + +-(void)keyboardDisplacementFix +{ + // https://stackoverflow.com/a/9637807/824966 + [UIView animateWithDuration:.25 animations:^{ + self.webView.scrollView.contentOffset = CGPointMake(0, 0); + }]; + +} + - (void)onSocketError:(NSNotification *)notification { [self loadErrorPage:nil]; }