From a67056816ee7fd2b8c74eef1854f2effbd145aea Mon Sep 17 00:00:00 2001 From: Brent Date: Tue, 20 Nov 2018 18:18:31 -0700 Subject: [PATCH] fix(ios): fix keyboard displacement bug in iOS 12 WKWebView (#201) * fix(ios): fix keyboard displacement bug in iOS 12 WKWebView * add debounce support for cancels on keyboardWillShow to fix bounce during programmatic focus toggling * simplify by applying to webView.scrollView instead of iterating subviews --- src/ios/CDVWKWebViewEngine.m | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) 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]; }