react-native-navigation的迁移库

RNNRootViewController.m 7.4KB

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