react-native-navigation的迁移库

RNNRootViewController.m 7.3KB

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