react-native-navigation的迁移库

RNNRootViewController.m 5.7KB

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