feat(WKWebview): Add fix for keyboard dismiss leaving viewport shifted in iOS 12 (#134)

This commit is contained in:
Tom
2018-11-19 11:21:36 +01:00
committed by Thibault Malbranche
parent d398323b5b
commit 5425039ff4
+33
View File
@@ -11,6 +11,7 @@
#import "objc/runtime.h"
static NSTimer *keyboardTimer;
static NSString *const MessageHanderName = @"ReactNative";
// runtime trick to remove WKWebView keyboard default toolbar
@@ -74,6 +75,19 @@ static NSString *const MessageHanderName = @"ReactNative";
_automaticallyAdjustContentInsets = YES;
_contentInset = UIEdgeInsetsZero;
}
// Workaround for a keyboard dismissal bug present in iOS 12
// https://openradar.appspot.com/radar?id=5018321736957952
if (@available(iOS 12.0, *)) {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification object:nil];
}
return self;
}
@@ -122,6 +136,25 @@ static NSString *const MessageHanderName = @"ReactNative";
}
}
-(void)keyboardWillHide
{
keyboardTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false];
[[NSRunLoop mainRunLoop] addTimer:keyboardTimer forMode:NSRunLoopCommonModes];
}
-(void)keyboardWillShow
{
if (keyboardTimer != nil) {
[keyboardTimer invalidate];
}
}
-(void)keyboardDisplacementFix
{
// https://stackoverflow.com/a/9637807/824966
[UIView animateWithDuration:.25 animations:^{
self.webView.scrollView.contentOffset = CGPointMake(0, 0);
}];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqual:@"estimatedProgress"] && object == self.webView) {
if(_onLoadingProgress){