react-native-navigation的迁移库

RNNRootViewController.m 8.2KB

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