react-native-navigation的迁移库

RNNRootViewController.m 7.5KB

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