react-native-navigation的迁移库

RNNRootViewController.m 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.view = [creator createRootView:self.componentName rootViewId:self.componentId];
  28. }
  29. [[NSNotificationCenter defaultCenter] addObserver:self
  30. selector:@selector(onJsReload)
  31. name:RCTJavaScriptWillStartLoadingNotification
  32. object:nil];
  33. self.navigationController.delegate = self;
  34. return self;
  35. }
  36. -(void)viewWillAppear:(BOOL)animated{
  37. [super viewWillAppear:animated];
  38. [self.options applyOn:self];
  39. }
  40. -(void)viewDidAppear:(BOOL)animated {
  41. [super viewDidAppear:animated];
  42. [self.eventEmitter sendComponentDidAppear:self.componentId componentName:self.componentName];
  43. }
  44. - (void)viewWillDisappear:(BOOL)animated {
  45. [super viewWillDisappear:animated];
  46. }
  47. -(void)viewDidDisappear:(BOOL)animated {
  48. [super viewDidDisappear:animated];
  49. [self.eventEmitter sendComponentDidDisappear:self.componentId componentName:self.componentName];
  50. }
  51. - (void)viewDidLoad {
  52. [super viewDidLoad];
  53. }
  54. - (void)optionsUpdated {
  55. [self setCustomNavigationTitleView];
  56. [self setCustomNavigationBarView];
  57. [self setCustomNavigationComponentBackground];
  58. }
  59. - (void)mergeOptions:(NSDictionary *)options {
  60. [self.options mergeIfEmptyWith:options];
  61. }
  62. - (void)setCustomNavigationTitleView {
  63. if (self.options.topBar.title.component.name) {
  64. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.title.component];
  65. RNNCustomTitleView *titleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:self.options.topBar.title.component.alignment];
  66. reactView.backgroundColor = UIColor.clearColor;
  67. titleView.backgroundColor = UIColor.clearColor;
  68. self.navigationItem.titleView = titleView;
  69. } if ([self.navigationItem.title isKindOfClass:[RNNCustomTitleView class]]) {
  70. self.navigationItem.title = nil;
  71. }
  72. }
  73. - (void)setCustomNavigationBarView {
  74. if (self.options.topBar.component.name) {
  75. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.component];
  76. RNNCustomTitleView *titleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  77. reactView.backgroundColor = UIColor.clearColor;
  78. titleView.backgroundColor = UIColor.clearColor;
  79. [self.navigationController.navigationBar addSubview:titleView];
  80. } else if ([[self.navigationController.navigationBar.subviews lastObject] isKindOfClass:[RNNCustomTitleView class]]) {
  81. [[self.navigationController.navigationBar.subviews lastObject] removeFromSuperview];
  82. }
  83. }
  84. - (void)setCustomNavigationComponentBackground {
  85. if (self.options.topBar.background.component.name) {
  86. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.background.component];
  87. RNNCustomTitleView *titleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  88. [self.navigationController.navigationBar insertSubview:titleView atIndex:1];
  89. self.navigationController.navigationBar.clipsToBounds = YES;
  90. } else if ([[self.navigationController.navigationBar.subviews objectAtIndex:1] isKindOfClass:[RNNCustomTitleView class]]) {
  91. [[self.navigationController.navigationBar.subviews objectAtIndex:1] removeFromSuperview];
  92. self.navigationController.navigationBar.clipsToBounds = NO;
  93. }
  94. }
  95. -(BOOL)isCustomTransitioned {
  96. return self.options.customTransition.animations != nil;
  97. }
  98. - (BOOL)isAnimated {
  99. return self.options.animated ? [self.options.animated boolValue] : YES;
  100. }
  101. - (BOOL)isCustomViewController {
  102. return self.isExternalComponent;
  103. }
  104. - (BOOL)prefersStatusBarHidden {
  105. if ([self.options.statusBarHidden boolValue]) {
  106. return YES;
  107. } else if ([self.options.statusBarHideWithTopBar boolValue]) {
  108. return self.navigationController.isNavigationBarHidden;
  109. }
  110. return NO;
  111. }
  112. - (UIStatusBarStyle)preferredStatusBarStyle {
  113. if (self.options.statusBarStyle && [self.options.statusBarStyle isEqualToString:@"light"]) {
  114. return UIStatusBarStyleLightContent;
  115. } else {
  116. return UIStatusBarStyleDefault;
  117. }
  118. }
  119. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  120. return self.options.supportedOrientations;
  121. }
  122. - (BOOL)hidesBottomBarWhenPushed
  123. {
  124. if (self.options.bottomTabs && self.options.bottomTabs.visible) {
  125. return ![self.options.bottomTabs.visible boolValue];
  126. }
  127. return NO;
  128. }
  129. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  130. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  131. if (![vc.options.backButtonTransition isEqualToString:@"custom"]){
  132. navigationController.delegate = nil;
  133. }
  134. }
  135. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  136. animationControllerForOperation:(UINavigationControllerOperation)operation
  137. fromViewController:(UIViewController*)fromVC
  138. toViewController:(UIViewController*)toVC {
  139. {
  140. if (self.animator) {
  141. return self.animator;
  142. } else if (operation == UINavigationControllerOperationPush && self.options.animations.push) {
  143. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.push];
  144. } else if (operation == UINavigationControllerOperationPop && self.options.animations.pop) {
  145. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.pop];
  146. } else {
  147. return nil;
  148. }
  149. }
  150. return nil;
  151. }
  152. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  153. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.showModal isDismiss:NO];
  154. }
  155. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  156. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.dismissModal isDismiss:YES];
  157. }
  158. -(void)applyTabBarItem {
  159. [self.options.bottomTab applyOn:self];
  160. }
  161. -(void)applyTopTabsOptions {
  162. [self.options.topTab applyOn:self];
  163. }
  164. /**
  165. * fix for #877, #878
  166. */
  167. -(void)onJsReload {
  168. [self cleanReactLeftovers];
  169. }
  170. /**
  171. * fix for #880
  172. */
  173. -(void)dealloc {
  174. [self cleanReactLeftovers];
  175. }
  176. -(void)cleanReactLeftovers {
  177. [[NSNotificationCenter defaultCenter] removeObserver:self];
  178. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  179. self.view = nil;
  180. }
  181. @end