react-native-navigation的迁移库

RNNSwizzles.m 5.0KB

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