Skip to content

Commit

Permalink
fix(ios): fix keyboard displacement bug in iOS 12 WKWebView (#201)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
booleanbetrayal authored and mlynch committed Nov 21, 2018
1 parent c93299e commit a670568
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/ios/CDVWKWebViewEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ @implementation CDVWKWebViewEngine

@synthesize engineWebView = _engineWebView;

NSTimer *timer;

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super init];
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
}
Expand Down

0 comments on commit a670568

Please sign in to comment.