react-native-navigation的迁移库

RNNSwizzles.m 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // RNNSwizzles.m
  3. // ReactNativeNavigation
  4. //
  5. // Created by Leo Natan (Wix) on 1/17/18.
  6. // Copyright © 2018 artal. All rights reserved.
  7. //
  8. #import "RNNSwizzles.h"
  9. @import ObjectiveC;
  10. @import UIKit;
  11. @implementation RNNSwizzles
  12. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_10_3
  13. static id (*__SWZ_initWithEventDispatcher_orig)(id self, SEL _cmd, id eventDispatcher);
  14. static void (*__SWZ_setFrame_orig)(id self, SEL _cmd, CGRect frame);
  15. static void __RNN_setFrame_orig(UIScrollView* self, SEL _cmd, CGRect frame)
  16. {
  17. CGPoint originalOffset = self.contentOffset;
  18. __SWZ_setFrame_orig(self, _cmd, frame);
  19. UIEdgeInsets contentInset;
  20. if (@available(iOS 11.0, *)) {
  21. contentInset = self.adjustedContentInset;
  22. } else {
  23. contentInset = self.contentInset;
  24. }
  25. CGSize contentSize = self.contentSize;
  26. // If contentSize has not been measured yet we can't check bounds.
  27. if (CGSizeEqualToSize(contentSize, CGSizeZero))
  28. {
  29. self.contentOffset = originalOffset;
  30. }
  31. else
  32. {
  33. // Make sure offset don't exceed bounds. This could happen on screen rotation.
  34. CGSize boundsSize = self.bounds.size;
  35. self.contentOffset = CGPointMake(MAX(-contentInset.left, MIN(contentSize.width - boundsSize.width + contentInset.right, originalOffset.x)),
  36. MAX(-contentInset.top, MIN(contentSize.height - boundsSize.height + contentInset.bottom, originalOffset.y)));
  37. }
  38. }
  39. - (id)__swz_initWithEventDispatcher:(id)eventDispatcher
  40. {
  41. id returnValue = __SWZ_initWithEventDispatcher_orig(self, _cmd, eventDispatcher);
  42. if (@available(iOS 11.0, *)) {
  43. [(UIScrollView*)[returnValue valueForKey:@"scrollView"] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentScrollableAxes];
  44. }
  45. return returnValue;
  46. }
  47. #endif
  48. + (void)applySwizzles
  49. {
  50. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_10_3
  51. static dispatch_once_t onceToken;
  52. dispatch_once(&onceToken, ^{
  53. Class cls = NSClassFromString(@"RCTScrollView");
  54. if(cls == NULL)
  55. {
  56. return;
  57. }
  58. Method m1 = class_getInstanceMethod(cls, NSSelectorFromString(@"initWithEventDispatcher:"));
  59. if(m1 == NULL)
  60. {
  61. return;
  62. }
  63. __SWZ_initWithEventDispatcher_orig = (void*)method_getImplementation(m1);
  64. Method m2 = class_getInstanceMethod([RNNSwizzles class], NSSelectorFromString(@"__swz_initWithEventDispatcher:"));
  65. method_exchangeImplementations(m1, m2);
  66. if (@available(iOS 11.0, *)) {
  67. cls = NSClassFromString(@"RCTCustomScrollView");
  68. if(cls == NULL)
  69. {
  70. return;
  71. }
  72. m1 = class_getInstanceMethod(cls, @selector(setFrame:));
  73. __SWZ_setFrame_orig = (void*)method_getImplementation(m1);
  74. method_setImplementation(m1, (IMP)__RNN_setFrame_orig);
  75. }
  76. });
  77. #endif
  78. }
  79. @end
  80. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
  81. @interface UIView (OS10SafeAreaSupport) @end
  82. @implementation UIView (OS10SafeAreaSupport)
  83. - (UIEdgeInsets)_ln_safeAreaInsets
  84. {
  85. static NSString* const b64 = @"X3ZpZXdDb250cm9sbGVyRm9yQW5jZXN0b3I=";
  86. UIViewController* vc = [self valueForKey:[[NSString alloc] initWithData:[[NSData alloc] initWithBase64EncodedString:b64 options:0] encoding:NSUTF8StringEncoding]];
  87. if(vc == nil)
  88. {
  89. return UIEdgeInsetsZero;
  90. }
  91. CGRect myFrameInVCView = [vc.view convertRect:self.bounds fromView:self];
  92. UIEdgeInsets rv = UIEdgeInsetsZero;
  93. rv.top = CGRectIntersection(myFrameInVCView, CGRectMake(0, 0, vc.view.bounds.size.width, vc.topLayoutGuide.length)).size.height;
  94. rv.bottom = CGRectIntersection(myFrameInVCView, CGRectMake(0, vc.view.bounds.size.height - vc.bottomLayoutGuide.length, vc.view.bounds.size.width, vc.bottomLayoutGuide.length)).size.height;
  95. return rv;
  96. }
  97. - (void)_ln_triggerSafeAreaInsetsDidChange
  98. {
  99. if([self respondsToSelector:@selector(safeAreaInsetsDidChange)])
  100. {
  101. [self performSelector:@selector(safeAreaInsetsDidChange)];
  102. }
  103. }
  104. - (void)_ln_layoutSubviews
  105. {
  106. [self _ln_triggerSafeAreaInsetsDidChange];
  107. struct objc_super super = {.receiver = self, .super_class = class_getSuperclass(object_getClass(self))};
  108. void (*super_class)(struct objc_super*, SEL) = (void*)objc_msgSendSuper;
  109. super_class(&super, _cmd);
  110. }
  111. - (void)_ln_setFrame:(CGRect)frame
  112. {
  113. [self _ln_triggerSafeAreaInsetsDidChange];
  114. struct objc_super super = {.receiver = self, .super_class = class_getSuperclass(object_getClass(self))};
  115. void (*super_class)(struct objc_super*, SEL, CGRect) = (void*)objc_msgSendSuper;
  116. super_class(&super, _cmd, frame);
  117. }
  118. - (void)_ln_setCenter:(CGPoint)center
  119. {
  120. [self _ln_triggerSafeAreaInsetsDidChange];
  121. struct objc_super super = {.receiver = self, .super_class = class_getSuperclass(object_getClass(self))};
  122. void (*super_class)(struct objc_super*, SEL, CGPoint) = (void*)objc_msgSendSuper;
  123. super_class(&super, _cmd, center);
  124. }
  125. - (void)_ln_setBounds:(CGRect)bounds
  126. {
  127. [self _ln_triggerSafeAreaInsetsDidChange];
  128. struct objc_super super = {.receiver = self, .super_class = class_getSuperclass(object_getClass(self))};
  129. void (*super_class)(struct objc_super*, SEL, CGRect) = (void*)objc_msgSendSuper;
  130. super_class(&super, _cmd, bounds);
  131. }
  132. + (void)load
  133. {
  134. static dispatch_once_t onceToken;
  135. dispatch_once(&onceToken, ^{
  136. if(NSProcessInfo.processInfo.operatingSystemVersion.majorVersion < 11)
  137. {
  138. Class cls = NSClassFromString(@"RCTSafeAreaView");
  139. if(cls == NULL)
  140. {
  141. return;
  142. }
  143. Method m = class_getInstanceMethod([UIView class], @selector(_ln_safeAreaInsets));
  144. if(NO == class_addMethod(cls, @selector(safeAreaInsets), method_getImplementation(m), method_getTypeEncoding(m)))
  145. {
  146. return;
  147. }
  148. m = class_getInstanceMethod([UIView class], @selector(_ln_layoutSubviews));
  149. if(NO == class_addMethod(cls, @selector(layoutSubviews), method_getImplementation(m), method_getTypeEncoding(m)))
  150. {
  151. return;
  152. }
  153. m = class_getInstanceMethod([UIView class], @selector(_ln_setFrame:));
  154. if(NO == class_addMethod(cls, @selector(setFrame:), method_getImplementation(m), method_getTypeEncoding(m)))
  155. {
  156. return;
  157. }
  158. m = class_getInstanceMethod([UIView class], @selector(_ln_setCenter:));
  159. if(NO == class_addMethod(cls, @selector(setCenter:), method_getImplementation(m), method_getTypeEncoding(m)))
  160. {
  161. return;
  162. }
  163. m = class_getInstanceMethod([UIView class], @selector(_ln_setBounds:));
  164. if(NO == class_addMethod(cls, @selector(setBounds:), method_getImplementation(m), method_getTypeEncoding(m)))
  165. {
  166. return;
  167. }
  168. }
  169. });
  170. }
  171. @end
  172. #endif