react-native-navigation的迁移库

RNNRootViewController.m 7.5KB

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