react-native-navigation的迁移库

RNNRootViewController.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #import "RNNRootViewController.h"
  2. #import <React/RCTConvert.h>
  3. #import "RNNAnimator.h"
  4. #import "RNNCustomTitleView.h"
  5. #import "RNNPushAnimation.h"
  6. @interface RNNRootViewController()
  7. @property (nonatomic, strong) NSString* componentName;
  8. @property (nonatomic) BOOL _statusBarHidden;
  9. @property (nonatomic) BOOL isExternalComponent;
  10. @end
  11. @implementation RNNRootViewController
  12. -(instancetype)initWithName:(NSString*)name
  13. withOptions:(RNNNavigationOptions*)options
  14. withComponentId:(NSString*)componentId
  15. rootViewCreator:(id<RNNRootViewCreator>)creator
  16. eventEmitter:(RNNEventEmitter*)eventEmitter
  17. isExternalComponent:(BOOL)isExternalComponent {
  18. self = [super init];
  19. self.componentId = componentId;
  20. self.componentName = name;
  21. self.options = options;
  22. self.eventEmitter = eventEmitter;
  23. self.animator = [[RNNAnimator alloc] initWithTransitionOptions:self.options.customTransition];
  24. self.creator = creator;
  25. self.isExternalComponent = isExternalComponent;
  26. if (self.isExternalComponent) {
  27. [self addExternalVC:name];
  28. } else {
  29. self.view = [creator createRootView:self.componentName rootViewId:self.componentId];
  30. }
  31. [[NSNotificationCenter defaultCenter] addObserver:self
  32. selector:@selector(onJsReload)
  33. name:RCTJavaScriptWillStartLoadingNotification
  34. object:nil];
  35. self.navigationController.delegate = self;
  36. return self;
  37. }
  38. -(void)viewWillAppear:(BOOL)animated{
  39. [super viewWillAppear:animated];
  40. [self.options applyOn:self];
  41. [self setCustomNavigationTitleView];
  42. [self setCustomNavigationBarView];
  43. }
  44. -(void)viewDidAppear:(BOOL)animated {
  45. [super viewDidAppear:animated];
  46. [self.eventEmitter sendComponentDidAppear:self.componentId];
  47. }
  48. - (void)viewWillDisappear:(BOOL)animated {
  49. [super viewWillDisappear:animated];
  50. }
  51. -(void)viewDidDisappear:(BOOL)animated {
  52. [super viewDidDisappear:animated];
  53. [self.eventEmitter sendComponentDidDisappear:self.componentId];
  54. }
  55. - (void)viewDidLoad {
  56. [super viewDidLoad];
  57. }
  58. - (void)mergeOptions:(NSDictionary *)options {
  59. [self.options mergeIfEmptyWith:options];
  60. }
  61. - (void)setCustomNavigationTitleView {
  62. if (self.options.topBar.customTitleViewName) {
  63. UIView *reactView = [_creator createRootView:self.options.topBar.customTitleViewName rootViewId:self.options.topBar.customTitleViewName];
  64. RNNCustomTitleView *titleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:nil];
  65. self.navigationItem.titleView = titleView;
  66. }
  67. }
  68. - (void)setCustomNavigationBarView {
  69. if (self.options.topBar.customViewName) {
  70. UIView *reactView = [_creator createRootView:self.options.topBar.customViewName rootViewId:@"navBar"];
  71. RNNCustomTitleView *titleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:nil];
  72. [self.navigationController.navigationBar addSubview:titleView];
  73. }
  74. }
  75. -(BOOL)isCustomTransitioned {
  76. return self.options.customTransition.animations != nil;
  77. }
  78. - (BOOL)isAnimated {
  79. return self.options.animated ? [self.options.animated boolValue] : YES;
  80. }
  81. - (BOOL)isCustomViewController {
  82. return self.isExternalComponent;
  83. }
  84. - (BOOL)prefersStatusBarHidden {
  85. if ([self.options.statusBarHidden boolValue]) {
  86. return YES;
  87. } else if ([self.options.statusBarHideWithTopBar boolValue]) {
  88. return self.navigationController.isNavigationBarHidden;
  89. }
  90. return NO;
  91. }
  92. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  93. return self.options.supportedOrientations;
  94. }
  95. - (BOOL)hidesBottomBarWhenPushed
  96. {
  97. if (self.options.bottomTabs && self.options.bottomTabs.visible) {
  98. return ![self.options.bottomTabs.visible boolValue];
  99. }
  100. return NO;
  101. }
  102. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  103. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  104. if (![vc.options.backButtonTransition isEqualToString:@"custom"]){
  105. navigationController.delegate = nil;
  106. }
  107. }
  108. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  109. animationControllerForOperation:(UINavigationControllerOperation)operation
  110. fromViewController:(UIViewController*)fromVC
  111. toViewController:(UIViewController*)toVC {
  112. {
  113. if (self.animator) {
  114. return self.animator;
  115. } else if (operation == UINavigationControllerOperationPush && self.options.animations.push) {
  116. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.push];
  117. } else if (operation == UINavigationControllerOperationPop && self.options.animations.pop) {
  118. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.pop];
  119. } else {
  120. return nil;
  121. }
  122. }
  123. return nil;
  124. }
  125. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  126. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.showModal isDismiss:NO];
  127. }
  128. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  129. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.dismissModal isDismiss:YES];
  130. }
  131. -(void)applyTabBarItem {
  132. [self.options.bottomTab applyOn:self];
  133. }
  134. -(void)applyTopTabsOptions {
  135. [self.options.topTab applyOn:self];
  136. }
  137. -(void)addExternalVC:(NSString*)className {
  138. if (className != nil) {
  139. Class class = NSClassFromString(className);
  140. if (class != NULL) {
  141. id obj = [[class alloc] init];
  142. if (obj != nil && [obj isKindOfClass:[UIViewController class]]) {
  143. UIViewController *viewController = (UIViewController*)obj;
  144. [self addChildViewController:viewController];
  145. self.view = [[UIView alloc] init];
  146. self.view.backgroundColor = [UIColor whiteColor];
  147. [self.view addSubview:viewController.view];
  148. }
  149. else {
  150. NSLog(@"addExternalVC: could not create instance. Make sure that your class is a UIViewController whihc confirms to RCCExternalViewControllerProtocol");
  151. }
  152. }
  153. else {
  154. NSLog(@"addExternalVC: could not create class from string. Check that the proper class name wass passed in ExternalNativeScreenClass");
  155. }
  156. }
  157. }
  158. /**
  159. * fix for #877, #878
  160. */
  161. -(void)onJsReload {
  162. [self cleanReactLeftovers];
  163. }
  164. /**
  165. * fix for #880
  166. */
  167. -(void)dealloc {
  168. [self cleanReactLeftovers];
  169. }
  170. -(void)cleanReactLeftovers {
  171. [[NSNotificationCenter defaultCenter] removeObserver:self];
  172. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  173. self.view = nil;
  174. }
  175. @end