From 1cb767dbc775ae8ae7dcafa7c27ca2c6b90c21c1 Mon Sep 17 00:00:00 2001 From: Manuel Mtz-Almeida Date: Wed, 20 Sep 2017 20:33:25 +0200 Subject: [PATCH] Fix for iOS11 initial scroll --- src/ios/CDVWKWebViewEngine.m | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/ios/CDVWKWebViewEngine.m b/src/ios/CDVWKWebViewEngine.m index 618a457..34d7930 100644 --- a/src/ios/CDVWKWebViewEngine.m +++ b/src/ios/CDVWKWebViewEngine.m @@ -30,6 +30,60 @@ Licensed to the Apache Software Foundation (ASF) under one #define CDV_IONIC_STOP_SCROLL @"stopScroll" +@implementation UIScrollView (BugIOS11) + +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 + ++ (void)load { + if (@available(iOS 11.0, *)) { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + Class class = [self class]; + SEL originalSelector = @selector(init); + SEL swizzledSelector = @selector(xxx_init); + + Method originalMethod = class_getInstanceMethod(class, originalSelector); + Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); + + BOOL didAddMethod = + class_addMethod(class, + originalSelector, + method_getImplementation(swizzledMethod), + method_getTypeEncoding(swizzledMethod)); + + if (didAddMethod) { + class_replaceMethod(class, + swizzledSelector, + method_getImplementation(originalMethod), + method_getTypeEncoding(originalMethod)); + } else { + method_exchangeImplementations(originalMethod, swizzledMethod); + } + }); + } +} + +#endif + +#pragma mark - Method Swizzling + +- (id)xxx_init { + id a = [self xxx_init]; + if (@available(iOS 11.0, *)) { + NSArray *stack = [NSThread callStackSymbols]; + for(NSString *trace in stack) { + if([trace containsString:@"WebKit"]) { + [a setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever]; + break; + } + } + } + return a; +} + +@end + + @interface CDVWKWeakScriptMessageHandler : NSObject @property (nonatomic, weak, readonly) idscriptMessageHandler;