@@ -114,8 +114,6 @@ @implementation CDVWKWebViewEngine
114
114
115
115
@synthesize engineWebView = _engineWebView;
116
116
117
- NSTimer *timer;
118
-
119
117
- (instancetype )initWithFrame : (CGRect)frame
120
118
{
121
119
self = [super init ];
@@ -303,15 +301,15 @@ - (void)pluginInitialize
303
301
selector: @selector (onAppWillEnterForeground: )
304
302
name: UIApplicationWillEnterForegroundNotification object: nil ];
305
303
306
- [[ NSNotificationCenter defaultCenter ]
307
- addObserver: self
308
- selector: @selector ( keyboardWillHide )
309
- name: UIKeyboardWillHideNotification object: nil ];
310
-
311
- [[ NSNotificationCenter defaultCenter ]
312
- addObserver: self
313
- selector: @selector ( keyboardWillShow )
314
- name: UIKeyboardWillShowNotification object: nil ];
304
+ // If less than ios 13.4
305
+ if (@ available (iOS 13.4 , *)) {} else {
306
+ // For keyboard dismissal leaving viewport shifted (can potentially be removed when apple releases the fix for the issue discussed here: https://github.com/apache/cordova-ios/issues/417#issuecomment-423340885 )
307
+ // Apple has released a fix in 13.4, but not in 12.x (as of 12.4.6)
308
+ [[ NSNotificationCenter defaultCenter ]
309
+ addObserver: self
310
+ selector: @selector ( keyboardWillHide )
311
+ name: UIKeyboardWillHideNotification object: nil ];
312
+ }
315
313
316
314
NSLog (@" Using Ionic WKWebView" );
317
315
@@ -378,27 +376,22 @@ - (void)onAppWillEnterForeground:(NSNotification *)notification {
378
376
379
377
-(void )keyboardWillHide
380
378
{
381
- if (@available (iOS 12.0 , *)) {
382
- timer = [NSTimer scheduledTimerWithTimeInterval: 0 target: self selector: @selector (keyboardDisplacementFix ) userInfo: nil repeats: false ];
383
- [[NSRunLoop mainRunLoop ] addTimer: timer forMode: NSRunLoopCommonModes ];
384
- }
385
- }
379
+ // For keyboard dismissal leaving viewport shifted (can potentially be removed when apple releases the fix for the issue discussed here: https://github.com/apache/cordova-ios/issues/417#issuecomment-423340885)
380
+ UIScrollView * scrollView = self.webView .scrollView ;
381
+ // Calculate some vars for convenience
382
+ CGFloat contentLengthWithInsets = scrollView.contentSize .height + scrollView.adjustedContentInset .top + scrollView.adjustedContentInset .bottom ;
383
+ CGFloat contentOffsetY = scrollView.contentOffset .y ;
384
+ CGFloat screenHeight = scrollView.frame .size .height ;
385
+ CGFloat maxAllowedOffsetY = fmax (contentLengthWithInsets - screenHeight, 0 ); // 0 is for the case where content is shorter than screen
386
386
387
- -(void )keyboardWillShow
388
- {
389
- if (timer != nil ) {
390
- [timer invalidate ];
387
+ // If the keyboard allowed the user to get to an offset beyond the max
388
+ if (contentOffsetY > maxAllowedOffsetY) {
389
+ // Reset the scroll to the max allowed so that there is no additional empty white space at the bottom where the keyboard occupied!
390
+ CGPoint bottomOfPage = CGPointMake (scrollView.contentOffset .x , maxAllowedOffsetY);
391
+ [scrollView setContentOffset: bottomOfPage];
391
392
}
392
393
}
393
394
394
- -(void )keyboardDisplacementFix
395
- {
396
- // https://stackoverflow.com/a/9637807/824966
397
- [UIView animateWithDuration: .25 animations: ^{
398
- self.webView .scrollView .contentOffset = CGPointMake (0 , 0 );
399
- }];
400
-
401
- }
402
395
- (BOOL )shouldReloadWebView
403
396
{
404
397
WKWebView * wkWebView = (WKWebView *)_engineWebView;
0 commit comments