react-native-navigation的迁移库

RNNSwizzles.m 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // RNNSwizzles.m
  3. // ReactNativeNavigation
  4. //
  5. // Created by Leo Natan (Wix) on 1/17/18.
  6. // Copyright © 2018 Wix. All rights reserved.
  7. //
  8. #import "RNNSwizzles.h"
  9. @import ObjectiveC;
  10. @import UIKit;
  11. static id (*__SWZ_initWithEventDispatcher_orig)(id self, SEL _cmd, id eventDispatcher);
  12. @implementation RNNSwizzles
  13. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_10_3
  14. - (id)__swz_initWithEventDispatcher:(id)eventDispatcher
  15. {
  16. id returnValue = __SWZ_initWithEventDispatcher_orig(self, _cmd, eventDispatcher);
  17. if (@available(iOS 11.0, *)) {
  18. [(UIScrollView*)[returnValue valueForKey:@"scrollView"] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentAlways];
  19. }
  20. return returnValue;
  21. }
  22. + (void)load
  23. {
  24. Class cls = NSClassFromString(@"RCTScrollView");
  25. if(cls == NULL)
  26. {
  27. return;
  28. }
  29. Method m1 = class_getInstanceMethod(cls, NSSelectorFromString(@"initWithEventDispatcher:"));
  30. if(m1 == NULL)
  31. {
  32. return;
  33. }
  34. __SWZ_initWithEventDispatcher_orig = (void*)method_getImplementation(m1);
  35. Method m2 = class_getInstanceMethod([RNNSwizzles class], NSSelectorFromString(@"__swz_initWithEventDispatcher:"));
  36. method_exchangeImplementations(m1, m2);
  37. }
  38. #endif
  39. @end
  40. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
  41. @interface UIView (OS10SafeAreaSupport) @end
  42. @implementation UIView (OS10SafeAreaSupport)
  43. - (UIEdgeInsets)_ln_safeAreaInsets
  44. {
  45. static NSString* const b64 = @"X3ZpZXdDb250cm9sbGVyRm9yQW5jZXN0b3I=";
  46. UIViewController* vc = [self valueForKey:[[NSString alloc] initWithData:[[NSData alloc] initWithBase64EncodedString:b64 options:0] encoding:NSUTF8StringEncoding]];
  47. if(vc == nil)
  48. {
  49. return UIEdgeInsetsZero;
  50. }
  51. CGRect myFrameInVCView = [vc.view convertRect:self.bounds fromView:self];
  52. UIEdgeInsets rv = UIEdgeInsetsZero;
  53. rv.top = CGRectIntersection(myFrameInVCView, CGRectMake(0, 0, vc.view.bounds.size.width, vc.topLayoutGuide.length)).size.height;
  54. rv.bottom = CGRectIntersection(myFrameInVCView, CGRectMake(0, vc.view.bounds.size.height - vc.bottomLayoutGuide.length, vc.view.bounds.size.width, vc.bottomLayoutGuide.length)).size.height;
  55. return rv;
  56. }
  57. - (void)_ln_triggerSafeAreaInsetsDidChange
  58. {
  59. if([self respondsToSelector:@selector(safeAreaInsetsDidChange)])
  60. {
  61. [self performSelector:@selector(safeAreaInsetsDidChange)];
  62. }
  63. }
  64. - (void)_ln_layoutSubviews
  65. {
  66. [self _ln_triggerSafeAreaInsetsDidChange];
  67. struct objc_super super = {.receiver = self, .super_class = class_getSuperclass(object_getClass(self))};
  68. void (*super_class)(struct objc_super*, SEL) = (void*)objc_msgSendSuper;
  69. super_class(&super, _cmd);
  70. }
  71. - (void)_ln_setFrame:(CGRect)frame
  72. {
  73. [self _ln_triggerSafeAreaInsetsDidChange];
  74. struct objc_super super = {.receiver = self, .super_class = class_getSuperclass(object_getClass(self))};
  75. void (*super_class)(struct objc_super*, SEL, CGRect) = (void*)objc_msgSendSuper;
  76. super_class(&super, _cmd, frame);
  77. }
  78. - (void)_ln_setCenter:(CGPoint)center
  79. {
  80. [self _ln_triggerSafeAreaInsetsDidChange];
  81. struct objc_super super = {.receiver = self, .super_class = class_getSuperclass(object_getClass(self))};
  82. void (*super_class)(struct objc_super*, SEL, CGPoint) = (void*)objc_msgSendSuper;
  83. super_class(&super, _cmd, center);
  84. }
  85. - (void)_ln_setBounds:(CGRect)bounds
  86. {
  87. [self _ln_triggerSafeAreaInsetsDidChange];
  88. struct objc_super super = {.receiver = self, .super_class = class_getSuperclass(object_getClass(self))};
  89. void (*super_class)(struct objc_super*, SEL, CGRect) = (void*)objc_msgSendSuper;
  90. super_class(&super, _cmd, bounds);
  91. }
  92. + (void)load
  93. {
  94. static dispatch_once_t onceToken;
  95. dispatch_once(&onceToken, ^{
  96. if(NSProcessInfo.processInfo.operatingSystemVersion.majorVersion < 11)
  97. {
  98. Class cls = NSClassFromString(@"RCTSafeAreaView");
  99. if(cls == NULL)
  100. {
  101. return;
  102. }
  103. Method m = class_getInstanceMethod([UIView class], @selector(_ln_safeAreaInsets));
  104. if(NO == class_addMethod(cls, @selector(safeAreaInsets), method_getImplementation(m), method_getTypeEncoding(m)))
  105. {
  106. return;
  107. }
  108. m = class_getInstanceMethod([UIView class], @selector(_ln_layoutSubviews));
  109. if(NO == class_addMethod(cls, @selector(layoutSubviews), method_getImplementation(m), method_getTypeEncoding(m)))
  110. {
  111. return;
  112. }
  113. m = class_getInstanceMethod([UIView class], @selector(_ln_setFrame:));
  114. if(NO == class_addMethod(cls, @selector(setFrame:), method_getImplementation(m), method_getTypeEncoding(m)))
  115. {
  116. return;
  117. }
  118. m = class_getInstanceMethod([UIView class], @selector(_ln_setCenter:));
  119. if(NO == class_addMethod(cls, @selector(setCenter:), method_getImplementation(m), method_getTypeEncoding(m)))
  120. {
  121. return;
  122. }
  123. m = class_getInstanceMethod([UIView class], @selector(_ln_setBounds:));
  124. if(NO == class_addMethod(cls, @selector(setBounds:), method_getImplementation(m), method_getTypeEncoding(m)))
  125. {
  126. return;
  127. }
  128. }
  129. });
  130. }
  131. @end
  132. #endif