react-native-navigation的迁移库

RNNRootViewController.m 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. @property (nonatomic) BOOL _optionsApplied;
  11. @property (nonatomic, copy) void (^rotationBlock)(void);
  12. @end
  13. @implementation RNNRootViewController
  14. -(instancetype)initWithName:(NSString*)name
  15. withOptions:(RNNNavigationOptions*)options
  16. withComponentId:(NSString*)componentId
  17. rootViewCreator:(id<RNNRootViewCreator>)creator
  18. eventEmitter:(RNNEventEmitter*)eventEmitter
  19. isExternalComponent:(BOOL)isExternalComponent {
  20. self = [super init];
  21. self.componentId = componentId;
  22. self.componentName = name;
  23. self.options = options;
  24. self.eventEmitter = eventEmitter;
  25. self.animator = [[RNNAnimator alloc] initWithTransitionOptions:self.options.customTransition];
  26. self.creator = creator;
  27. self.isExternalComponent = isExternalComponent;
  28. if (!self.isExternalComponent) {
  29. self.view = [creator createRootView:self.componentName rootViewId:self.componentId];
  30. }
  31. [[NSNotificationCenter defaultCenter] addObserver:self
  32. selector:@selector(onJsReload)
  33. name:RCTJavaScriptWillStartLoadingNotification
  34. object:nil];
  35. self.navigationController.delegate = self;
  36. [[NSNotificationCenter defaultCenter] addObserver:self
  37. selector:@selector(orientationDidChange:)
  38. name:UIDeviceOrientationDidChangeNotification
  39. object:nil];
  40. return self;
  41. }
  42. -(void)viewWillAppear:(BOOL)animated{
  43. [super viewWillAppear:animated];
  44. if (!self._optionsApplied) {
  45. [self.options applyOn:self];
  46. }
  47. self._optionsApplied = true;
  48. }
  49. -(void)viewDidAppear:(BOOL)animated {
  50. [super viewDidAppear:animated];
  51. [self.eventEmitter sendComponentDidAppear:self.componentId componentName:self.componentName];
  52. }
  53. - (void)viewWillDisappear:(BOOL)animated {
  54. [super viewWillDisappear:animated];
  55. }
  56. -(void)viewDidDisappear:(BOOL)animated {
  57. [super viewDidDisappear:animated];
  58. [self.eventEmitter sendComponentDidDisappear:self.componentId componentName:self.componentName];
  59. }
  60. - (void)viewDidLoad {
  61. [super viewDidLoad];
  62. }
  63. - (void)optionsUpdated {
  64. [self setCustomNavigationTitleView];
  65. [self setCustomNavigationBarView];
  66. [self setCustomNavigationComponentBackground];
  67. }
  68. - (void)mergeOptions:(NSDictionary *)options {
  69. [self.options mergeIfEmptyWith:options];
  70. }
  71. - (void)setCustomNavigationTitleView {
  72. if (self.options.topBar.title.component.name) {
  73. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.title.component];
  74. RNNCustomTitleView *titleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:self.options.topBar.title.component.alignment];
  75. reactView.backgroundColor = UIColor.clearColor;
  76. titleView.backgroundColor = UIColor.clearColor;
  77. self.navigationItem.titleView = titleView;
  78. } if ([self.navigationItem.title isKindOfClass:[RNNCustomTitleView class]]) {
  79. self.navigationItem.title = nil;
  80. }
  81. }
  82. - (void)setCustomNavigationBarView {
  83. if (self.options.topBar.component.name) {
  84. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.component];
  85. RNNCustomTitleView *titleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  86. reactView.backgroundColor = UIColor.clearColor;
  87. titleView.backgroundColor = UIColor.clearColor;
  88. [self.navigationController.navigationBar addSubview:titleView];
  89. } else if ([[self.navigationController.navigationBar.subviews lastObject] isKindOfClass:[RNNCustomTitleView class]]) {
  90. [[self.navigationController.navigationBar.subviews lastObject] removeFromSuperview];
  91. }
  92. }
  93. - (void)setCustomNavigationComponentBackground {
  94. if (self.options.topBar.background.component.name) {
  95. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.background.component];
  96. RNNCustomTitleView *titleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  97. [self.navigationController.navigationBar insertSubview:titleView atIndex:1];
  98. self.navigationController.navigationBar.clipsToBounds = YES;
  99. } else if ([[self.navigationController.navigationBar.subviews objectAtIndex:1] isKindOfClass:[RNNCustomTitleView class]]) {
  100. [[self.navigationController.navigationBar.subviews objectAtIndex:1] removeFromSuperview];
  101. self.navigationController.navigationBar.clipsToBounds = NO;
  102. }
  103. }
  104. -(BOOL)isCustomTransitioned {
  105. return self.options.customTransition.animations != nil;
  106. }
  107. - (BOOL)isAnimated {
  108. return self.options.animated ? [self.options.animated boolValue] : YES;
  109. }
  110. - (BOOL)isCustomViewController {
  111. return self.isExternalComponent;
  112. }
  113. - (BOOL)prefersStatusBarHidden {
  114. if ([self.options.statusBarHidden boolValue]) {
  115. return YES;
  116. } else if ([self.options.statusBarHideWithTopBar boolValue]) {
  117. return self.navigationController.isNavigationBarHidden;
  118. }
  119. return NO;
  120. }
  121. - (UIStatusBarStyle)preferredStatusBarStyle {
  122. if (self.options.statusBarStyle && [self.options.statusBarStyle isEqualToString:@"light"]) {
  123. return UIStatusBarStyleLightContent;
  124. } else {
  125. return UIStatusBarStyleDefault;
  126. }
  127. }
  128. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  129. return self.options.supportedOrientations;
  130. }
  131. - (BOOL)hidesBottomBarWhenPushed
  132. {
  133. if (self.options.bottomTabs && self.options.bottomTabs.visible) {
  134. return ![self.options.bottomTabs.visible boolValue];
  135. }
  136. return NO;
  137. }
  138. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  139. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  140. if (![vc.options.backButtonTransition isEqualToString:@"custom"]){
  141. navigationController.delegate = nil;
  142. }
  143. }
  144. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  145. animationControllerForOperation:(UINavigationControllerOperation)operation
  146. fromViewController:(UIViewController*)fromVC
  147. toViewController:(UIViewController*)toVC {
  148. {
  149. if (self.animator) {
  150. return self.animator;
  151. } else if (operation == UINavigationControllerOperationPush && self.options.animations.push) {
  152. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.push];
  153. } else if (operation == UINavigationControllerOperationPop && self.options.animations.pop) {
  154. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.pop];
  155. } else {
  156. return nil;
  157. }
  158. }
  159. return nil;
  160. }
  161. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  162. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.showModal isDismiss:NO];
  163. }
  164. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  165. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.dismissModal isDismiss:YES];
  166. }
  167. -(void)applyTabBarItem {
  168. [self.options.bottomTab applyOn:self];
  169. }
  170. -(void)applyTopTabsOptions {
  171. [self.options.topTab applyOn:self];
  172. }
  173. - (void)performOnRotation:(void (^)(void))block {
  174. _rotationBlock = block;
  175. }
  176. - (void)orientationDidChange:(NSNotification*)notification {
  177. if (_rotationBlock) {
  178. _rotationBlock();
  179. }
  180. }
  181. /**
  182. * fix for #877, #878
  183. */
  184. -(void)onJsReload {
  185. [self cleanReactLeftovers];
  186. }
  187. /**
  188. * fix for #880
  189. */
  190. -(void)dealloc {
  191. [self cleanReactLeftovers];
  192. }
  193. -(void)cleanReactLeftovers {
  194. [[NSNotificationCenter defaultCenter] removeObserver:self];
  195. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  196. self.view = nil;
  197. }
  198. @end