react-native-navigation的迁移库

RNNRootViewController.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #import "RNNRootViewController.h"
  2. #import <React/RCTConvert.h>
  3. #import "RNNAnimator.h"
  4. #import "RNNCustomTitleView.h"
  5. @interface RNNRootViewController()
  6. @property (nonatomic, strong) NSString* componentName;
  7. @property (nonatomic) BOOL _statusBarHidden;
  8. @property (nonatomic) BOOL isExternalComponent;
  9. @end
  10. @implementation RNNRootViewController
  11. -(instancetype)initWithName:(NSString*)name
  12. withOptions:(RNNNavigationOptions*)options
  13. withComponentId:(NSString*)componentId
  14. rootViewCreator:(id<RNNRootViewCreator>)creator
  15. eventEmitter:(RNNEventEmitter*)eventEmitter
  16. isExternalComponent:(BOOL)isExternalComponent {
  17. self = [super init];
  18. self.componentId = componentId;
  19. self.componentName = name;
  20. self.options = options;
  21. self.eventEmitter = eventEmitter;
  22. self.animator = [[RNNAnimator alloc] initWithTransitionOptions:self.options.customTransition];
  23. self.creator = creator;
  24. self.isExternalComponent = isExternalComponent;
  25. if (self.isExternalComponent) {
  26. [self addExternalVC:name];
  27. } else {
  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.modalPresentationStyle = UIModalPresentationCustom;
  35. self.navigationController.delegate = self;
  36. return self;
  37. }
  38. -(void)viewWillAppear:(BOOL)animated{
  39. [super viewWillAppear:animated];
  40. [self.options applyOn:self];
  41. [self setCustomNavigationTitleView];
  42. [self setCustomNavigationBarView];
  43. }
  44. -(void)viewDidAppear:(BOOL)animated {
  45. [super viewDidAppear:animated];
  46. [self.eventEmitter sendComponentDidAppear:self.componentId];
  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];
  54. }
  55. - (void)viewDidLoad {
  56. [super viewDidLoad];
  57. }
  58. - (void)mergeOptions:(NSDictionary *)options {
  59. [self.options mergeIfEmptyWith:options];
  60. }
  61. - (void)setCustomNavigationTitleView {
  62. if (self.options.topBar.customTitleViewName) {
  63. UIView *reactView = [_creator createRootView:self.options.topBar.customTitleViewName rootViewId:self.options.topBar.customTitleViewName];
  64. RNNCustomTitleView *titleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:nil];
  65. self.navigationItem.titleView = titleView;
  66. }
  67. }
  68. - (void)setCustomNavigationBarView {
  69. if (self.options.topBar.customViewName) {
  70. UIView *reactView = [_creator createRootView:self.options.topBar.customViewName rootViewId:@"navBar"];
  71. RNNCustomTitleView *titleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:nil];
  72. [self.navigationController.navigationBar addSubview:titleView];
  73. }
  74. }
  75. -(BOOL)isCustomTransitioned {
  76. return self.options.customTransition != nil;
  77. }
  78. - (BOOL)isAnimated {
  79. return self.options.animated ? [self.options.animated boolValue] : YES;
  80. }
  81. - (BOOL)isCustomViewController {
  82. return self.isExternalComponent;
  83. }
  84. - (BOOL)prefersStatusBarHidden {
  85. if ([self.options.statusBarHidden boolValue]) {
  86. return YES;
  87. } else if ([self.options.statusBarHideWithTopBar boolValue]) {
  88. return self.navigationController.isNavigationBarHidden;
  89. }
  90. return NO;
  91. }
  92. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  93. return self.options.supportedOrientations;
  94. }
  95. - (BOOL)hidesBottomBarWhenPushed
  96. {
  97. if (self.options.bottomTabs && self.options.bottomTabs.visible) {
  98. return ![self.options.bottomTabs.visible boolValue];
  99. }
  100. return NO;
  101. }
  102. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  103. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  104. if (![vc.options.backButtonTransition isEqualToString:@"custom"]){
  105. navigationController.delegate = nil;
  106. }
  107. }
  108. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  109. animationControllerForOperation:(UINavigationControllerOperation)operation
  110. fromViewController:(UIViewController*)fromVC
  111. toViewController:(UIViewController*)toVC {
  112. {
  113. if (operation == UINavigationControllerOperationPush) {
  114. return self.animator;
  115. } else if (operation == UINavigationControllerOperationPop) {
  116. return self.animator;
  117. } else {
  118. return nil;
  119. }
  120. }
  121. return nil;
  122. }
  123. -(void)applyTabBarItem {
  124. [self.options.bottomTab applyOn:self];
  125. }
  126. -(void)applyTopTabsOptions {
  127. [self.options.topTab applyOn:self];
  128. }
  129. -(void)addExternalVC:(NSString*)className {
  130. if (className != nil) {
  131. Class class = NSClassFromString(className);
  132. if (class != NULL) {
  133. id obj = [[class alloc] init];
  134. if (obj != nil && [obj isKindOfClass:[UIViewController class]]) {
  135. UIViewController *viewController = (UIViewController*)obj;
  136. [self addChildViewController:viewController];
  137. self.view = [[UIView alloc] init];
  138. self.view.backgroundColor = [UIColor whiteColor];
  139. [self.view addSubview:viewController.view];
  140. }
  141. else {
  142. NSLog(@"addExternalVC: could not create instance. Make sure that your class is a UIViewController whihc confirms to RCCExternalViewControllerProtocol");
  143. }
  144. }
  145. else {
  146. NSLog(@"addExternalVC: could not create class from string. Check that the proper class name wass passed in ExternalNativeScreenClass");
  147. }
  148. }
  149. }
  150. /**
  151. * fix for #877, #878
  152. */
  153. -(void)onJsReload {
  154. [self cleanReactLeftovers];
  155. }
  156. /**
  157. * fix for #880
  158. */
  159. -(void)dealloc {
  160. [self cleanReactLeftovers];
  161. }
  162. -(void)cleanReactLeftovers {
  163. [[NSNotificationCenter defaultCenter] removeObserver:self];
  164. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  165. self.view = nil;
  166. }
  167. @end