Skip to content

Commit a670568

Browse files
booleanbetrayalmlynch
authored andcommitted
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
1 parent c93299e commit a670568

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/ios/CDVWKWebViewEngine.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ @implementation CDVWKWebViewEngine
126126

127127
@synthesize engineWebView = _engineWebView;
128128

129+
NSTimer *timer;
130+
129131
- (instancetype)initWithFrame:(CGRect)frame
130132
{
131133
self = [super init];
@@ -363,6 +365,17 @@ - (void)pluginInitialize
363365
selector:@selector(onSocketError:)
364366
name:@"socketInUseError" object:nil];
365367

368+
[[NSNotificationCenter defaultCenter]
369+
addObserver:self
370+
selector:@selector(keyboardWillHide)
371+
name:UIKeyboardWillHideNotification object:nil];
372+
373+
[[NSNotificationCenter defaultCenter]
374+
addObserver:self
375+
selector:@selector(keyboardWillShow)
376+
name:UIKeyboardWillShowNotification object:nil];
377+
378+
366379
NSLog(@"Using Ionic WKWebView");
367380

368381
[self addURLObserver];
@@ -448,6 +461,31 @@ - (void)onAppWillEnterForeground:(NSNotification *)notification {
448461
}
449462
}
450463

464+
465+
-(void)keyboardWillHide
466+
{
467+
if (@available(iOS 12.0, *)) {
468+
timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false];
469+
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
470+
}
471+
}
472+
473+
-(void)keyboardWillShow
474+
{
475+
if (timer != nil) {
476+
[timer invalidate];
477+
}
478+
}
479+
480+
-(void)keyboardDisplacementFix
481+
{
482+
// https://stackoverflow.com/a/9637807/824966
483+
[UIView animateWithDuration:.25 animations:^{
484+
self.webView.scrollView.contentOffset = CGPointMake(0, 0);
485+
}];
486+
487+
}
488+
451489
- (void)onSocketError:(NSNotification *)notification {
452490
[self loadErrorPage:nil];
453491
}

0 commit comments

Comments
 (0)